Announcement

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

  • Changing numeric name

    I have a dataset with country names (country is a long variable). I need to change some country names in order to merge this dataset with another that has byte country names. But some country names need to be changed. E.g., in master data set Serbia and Montenegro is the name of the country. But it is simply Serbia in the dataset that is to be merged. I can't use encode since the variable is numeric. I can't simply change the name in the editor for the same reason. Is there a way to change the name? Thanks,

    Ric Uslaner

  • #2
    you did not provide a data example but it sounds as though you have labels and these can certainly be changed - see
    Code:
    h label

    Comment


    • #3
      I tried this but I could not change the format of the country variable so that I could change Serbia and Montenegro to Serbia. The variable manager would not let me change the format from string to numeric. There must be some way to create a numeric variable for country.

      Comment


      • #4
        You are leaving it to everybody's imagination what your variable actually is because you have not shown us an actual example from your data set using the -dataex- command. Your description is not clear enough, and it is unlikely that any description in words ever will be. That is why you are not getting helpful responses so far. Since nobody can be sure what your starting point is, nobody can direct you from there to your goal.

        My best guess, and it is only a guess, is that you are starting with a value labeled numeric variable. That is, your country variable looks something like what you get from running:
        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input long country
        1
        3
        4
        2
        5
        end
        label values country country
        label def country 1 "France", modify
        label def country 2 "Germany", modify
        label def country 3 "Italy", modify
        label def country 4 "Serbia and Montenegro", modify
        label def country 5 "Slovenia", modify
        If that's right, you need to now create a true string variable containing the names of the countries, and you can then edit the names in that string variable:

        Code:
        //  CREATE A TRUE STRING VARIABLE FROM VALUE-LABELED NUMERIC
        decode country, gen(str_country)
        
        //  CHANGE THE NAME OF SERBIA AND MONTENEGRO
        replace str_country = "Serbia" if str_country == "Serbia and Montenegro"
        Now you may need to change other countries' names to match the other data set you want to merge with. But once you have done that, you must merge using the string variable. Do not attempt to merge the original numeric variables: you will get scrambled data if you try. If the other data set also has a value-labeled numeric variable, you must first create a true string variable there as well. Once you have accomplished the merging, you an, if you wish, drop the original country variables, and -encode- the string variables you created to get a new value-labeled numeric variable.

        I will also add that if there are many countries that are differently named in the two data sets, rather than manually editing all the changes, you install Rafal Raciborski's -kountry- (available from SSC) and use it to harmonize the names. It will take you a little while to learn to use -kountry-, but once you know it, it will save you plenty of time in the future when you work with data sets that name countries.

        Comment


        • #5
          Here is what I get from dataex:

          . dataex country

          ----------------------- copy starting from the next line -----------------------
          Code:
          * Example generated by -dataex-. For more info, type help dataex
          clear
          input str18 country
          " Germany"          
          "Algeria"           
          "Angola"            
          "Argentina"         
          "Australi"          
          "Austria"           
          "Bahamas"           
          "Bangladesh"        
          "Barbados"          
          "Belarus"           
          "Belgium"           
          "Bolivia"           
          "Botswana"          
          "Brazil"            
          "Britain"           
          "Bulgaria"          
          "Cameroon"          
          "Canada"            
          "Chile"             
          "China"             
          "Colombia"          
          "Costa Rica"        
          "Czech"             
          "Denmark"           
          "Dominican Republic"
          "Ecuador"           
          "Egypt"             
          "El Salvador"       
          "Estonia"           
          "Ethiopia"          
          "Finland"           
          "France"            
          "Ghana"             
          "Greece"            
          "Guatemala"         
          "Haiti"             
          "Honduras"          
          "Hong Kong"         
          "Hungary"           
          "Iceland"           
          "India"             
          "Indonesia"         
          "Iran"              
          "Iraq"              
          "Ireland"           
          "Israel"            
          "Italy"             
          "Ivory Coast"       
          "Jamaica"           
          "Japan"             
          "Jordan"            
          "Kenya"             
          "Kuwait"            
          "Latvia"            
          "Lebanon"           
          "Liberia"           
          "Lithuania"         
          "Luxembourg"        
          "Malawi"            
          "Malaysia"          
          "Malta"             
          "Mauritius"         
          "Mexico"            
          "Morocco"           
          "Namibia"           
          "Nepal"             
          "Netherlands"       
          "New Zealand"       
          "Nicaragua"         
          "Nigeria"           
          "Norway"            
          "Pakistan"          
          "Panama"            
          "Paraguay"          
          "Peru"              
          "Philippines"       
          "Poland"            
          "Portugal"          
          "Qatar"             
          "Romania"           
          "Russia"            
          "Saudi Arabia"      
          "Senegal"           
          "Serbia"            
          "Singapore"         
          "Slovakia"          
          "South Africa"      
          "South Korea"       
          "Spain"             
          "Sri Lanka"         
          "Sweden"            
          "Switzerland"       
          "Taiwan"            
          "Tanzania"          
          "Thailand"          
          "Trinidad"          
          "Tunisia"           
          "Turkey"            
          "USA"               
          "Uganda"            
          end
          ------------------ copy up to and including the previous line ------------------

          Listed 100 out of 108 observations
          Use the count() option to list more

          . When I try the following, here is what I get:

          . replace country_01 = "Serbia" if country_01 == "Serbia and Montenegro"
          (0 real changes made)

          . decode country, gen(str_country)
          not possible with string variable
          r(108);

          Comment


          • #6
            So, country_01 is, in fact, already a string variable. The reason your -replace- command doesn't make any changes is because there is no observation in the data set where country_01 == "Serbia and Montenegro". In fact, if you look at it, you will see that there is already an observation where country_01 == "Serbia". So there is nothing you need to do here. This data set is ready for -merge-ing.

            Perhaps it is the other data set where you have an entry with country_01 == "Serbia and Montenegro"? If it is already a string variable, then your -replace- command in #5 will do what you need it to. If it is a value-labeled numeric variable, the code I set out in #4 will make the necessary changes. Either way, you will then be able to -merge- appropriately.

            Comment

            Working...
            X