Announcement

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

  • Label numeric variable with string values

    Hello, I've seen several posts about this but none quite answered my question. I have a list of country identification numbers, and a matching list of country names. I would like to label the entire numeric id list using the string country_name variable. Defining each country label individually would be very time consuming. Please see below. Thank you!

    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str52 country_name int country_num
    "Afghanistan" 2
    "Albania" 4
    "Algeria" 59
    "American Samoa" 10
    "Andorra" 5
    "Angola" 3
    "Antigua and Barbuda" 11
    "Arab World" 6
    "Argentina" 8
    "Armenia" 9
    "Aruba" 1
    "Australia" 12
    "Austria" 13
    "Azerbaijan" 14
    "Bahamas, The" 22
    "Bahrain" 21
    "Bangladesh" 19


  • #2
    If you do not need to specifically use the numbering system in the country_num variable, then the simplest approach is

    Code:
    drop country_num
    encode counry_name, gen(country_num)
    which will give you a new numeric variable country_num, with labels attached, but the numbering scheme will be different from what you had.

    If you need to preserve the numbering scheme in country_num, then this will do it:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str52 country_name int country_num
    "Afghanistan" 2
    "Albania" 4
    "Algeria" 59
    "American Samoa" 10
    "Andorra" 5
    "Angola" 3
    "Antigua and Barbuda" 11
    "Arab World" 6
    "Argentina" 8
    "Armenia" 9
    "Aruba" 1
    "Australia" 12
    "Austria" 13
    "Azerbaijan" 14
    "Bahamas, The" 22
    "Bahrain" 21
    "Bangladesh" 19
    end
    
    isid country_name
    isid country_num
    forvalues i = 1/`=_N' {
        label define country `=country_num[`i']' `"`=country_name[`i']'"', modify
    }
    
    label values country_num country

    Comment


    • #3
      I needed to preserve the numbering scheme, so the second code worked perfectly. Thank you!

      Comment


      • #4
        This is what labmask (Stata Journal) does. It's Clyde Schechter's approach wrapped together with some useful checks and tweaks and given a help file.

        Example:

        Code:
        clear
        input str52 country_name int country_num
        "Afghanistan" 2
        "Albania" 4
        "Algeria" 59
        "American Samoa" 10
        "Andorra" 5
        "Angola" 3
        "Antigua and Barbuda" 11
        "Arab World" 6
        "Argentina" 8
        "Armenia" 9
        "Aruba" 1
        "Australia" 12
        "Austria" 13
        "Azerbaijan" 14
        "Bahamas, The" 22
        "Bahrain" 21
        "Bangladesh" 19
        end 
        
        labmask country_num, values(country_name) 
        
        list 
        
             +-------------------------------------------+
             |        country_name           country_num |
             |-------------------------------------------|
          1. |         Afghanistan           Afghanistan |
          2. |             Albania               Albania |
          3. |             Algeria               Algeria |
          4. |      American Samoa        American Samoa |
          5. |             Andorra               Andorra |
             |-------------------------------------------|
          6. |              Angola                Angola |
          7. | Antigua and Barbuda   Antigua and Barbuda |
          8. |          Arab World            Arab World |
          9. |           Argentina             Argentina |
         10. |             Armenia               Armenia |
             |-------------------------------------------|
         11. |               Aruba                 Aruba |
         12. |           Australia             Australia |
         13. |             Austria               Austria |
         14. |          Azerbaijan            Azerbaijan |
         15. |        Bahamas, The          Bahamas, The |
             |-------------------------------------------|
         16. |             Bahrain               Bahrain |
         17. |          Bangladesh            Bangladesh |
             +-------------------------------------------+
        You must install
        Code:
        labmask
        before you can use, say from


        Code:
        . search labmask, sj
        
        Search of official help files, FAQs, Examples, SJs, and STBs
        
        SJ-8-2  gr0034  . . . . . . . . . .  Speaking Stata: Between tables and graphs
                (help labmask, seqvar if installed) . . . . . . . . . . . .  N. J. Cox
                Q2/08   SJ 8(2):269--289
                outlines techniques for producing table-like graphs

        Comment

        Working...
        X