Announcement

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

  • Changing numeric label values while keeping existing label values

    Dear Statalists,

    I have a question regarding modifying label values. I have a string variable with label names from one dataset (i.e. country name). This variable will act as ID to merge with other dataset, thus I want to assign particular values based on its name. Encode will not work in my case as the label values won't match the values in the second dataset given they have the same name. However, without making it a numeric variable, I can't define the label value either. I would be very grateful if you can help me with this issue.

    Many thanks,
    Thanh

  • #2
    Please provide a simple example. I cannot figure out what exactly you are looking for form your description.

    Best
    Daniel

    Comment


    • #3
      Dear Daniel,

      Thank you for your response. For example, I have dataset 1 and dataset 2. Both contain variable country, which will act as identification to merge two dataset. In dataset 1, it contains both label names with label values (1 "Canada" 2 "UK" ....). In dataset 2, country is a string variable with only label name (Canada UK...). I actually found the solution using encode country, gen... label() , which enable me to assign the label values of country variable in dataset 2 to country variable in dataset 1. But if you know anyway to merge two datasets using the label name instead of label value, I would love to know.

      Many thanks,
      Thanh

      Comment


      • #4
        I was actually looking for a data(set) example; not for more verbal descriptions.

        Originally posted by Thanh Bui View Post
        But if you know anyway to merge two datasets using the label name instead of label value, I would love to know.
        Well, decode your numeric variable in dataset 1 and merge using the string version. Here is an example:

        Code:
        // simulate dataset2
        clear
        input str6 country_string
        "UK"
        "Canada"
        end
        
        // look at dataset 2
        list
        
        // save dataset 2 (as a temporary file because this is just an example)
        tempfile dataset2
        save "`dataset2'"
        
        // simulate dataset1
        clear
        input country_numeric
        1
        2
        end
        
        label define country_numeric 1 "Canada" 2 "UK"
        label values country_numeric country_numeric
        
        // look at dataset1
        list
        
        // now decode your numeric variable
        decode country_numeric , generate(country_string)
        
        // ... and merge on the string version
        merge m:1 country_string using "`dataset2'"
        
        // look at combined data
        list
        Best
        Daniel

        Comment


        • #5
          Dear Daniel,

          I'm sorry for not making it clearer by creating a data sample. I would follow your guidance next time. I tried your method, it works perfectly. Thank you for spending time to answer my question.

          Best regards,
          Thanh

          Comment

          Working...
          X