Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Collapsing unique country names and country IDs

    Hello,

    I am getting strange results when I try to collapse country data and I would like to ask your help...

    variables: CountryName, CountryID
    observations:over 20,000

    Goal: there are 115 unique countries in the data and I want to export the unique country names and their IDs.

    My data looks like this:
    CountryName CountryID
    Albania 100
    Germany 110
    ...etc. for 20,000 entires

    My collapse code:collapse (firstnm) CountryName, by(CountryID)

    Problem after this collapse code is that the "CountryID" is no longer a number...it only shows the string:
    CountryName CountryID
    Albania Albania
    Germany Germany
    ...etc.
    Does anyone have any suggestions on how to fix my -collapse- code? I want to be able to export this collapsed data with BOTH CountryName and CountryID.

    Thank you!

  • #2
    My data looks like this:
    Looks can be deceiving. Showing what the data looks like to your eye does not tell us what the data looks like to Stata. Show an example of the actual data by using the -dataex- command. Make sure the example you show exhibits the problem you are encountering.

    If you are running version 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    Comment


    • #3
      Your question really isn't clear without more detail, or at a minimum it is too difficult to guess at a good answer from what you have shared. Please help us help you.

      Even the best descriptions of data are no substitute for an actual example of the data. There are many ways your data might be organized that are consistent with your incomplete description. In order to get a helpful response, you need to show some example data.

      Be sure to use the dataex command to do this. If you are running version 15.1 or later, or a fully updated version 14.2, dataex is already part of your official Stata installation. If not, run ssc install dataex to get it. Either way, run help dataex and read the simple instructions for using it. dataex will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

      When asking for help with code, always show example data.

      Comment


      • #4
        William Lisowski Clyde Schechter

        Thank you so much for letting me know! I have never used -dataex- but I will from now on!

        I have actually solved the problem, which was not with -collapse- (the collapse worked correctly). The problem was with a later code where I was exporting and all I needed to do was add the option "nolabel" to the export command...this is why I thought I was seeing two columns of duplicates.

        Thank you both for your responses!

        Comment

        Working...
        X