Announcement

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

  • Label A Range

    I would like to label define a range of values in STATA. My data looks like this:
    Occupation Code Occupation Name
    201 Social Work
    202-206 Community/Social Services
    316 Physical Therapy
    420-425 Building & Grounds
    For single values like 201 and 316, I would simply write: label define occupation 201 "Social work" 316 "Physical Therapy"; But, how do I label define a range?

    Thanks.


  • #2
    You can do it in a short loop after you define the label for all the single values

    Code:
    forvalues i = 202/206 {
         label define occupation `i' "Community/Social Services", add
    }
    See -help forvalues-. This is a real "bread and butter" command in Stata that you really need to learn in order to function in Stata. See the help files and manual sections for -forvalues- and its companion -foreach-.

    Comment


    • #3
      I agree with Clyde's technique, but depending on where you are going with this, it might not be what you want. Consider the following example.
      Code:
      . input x
      
                   x
        1. 1
        2. 2
        3. 3
        4. 4
        5. 5
        6. end
      
      . label define xlab 1 "Dog" 2 "Cat" 3 "Cat" 4 "Cat" 5 "Dog"
      
      . label values x xlab
      
      . tab x
      
                x |      Freq.     Percent        Cum.
      ------------+-----------------------------------
              Dog |          1       20.00       20.00
              Cat |          1       20.00       40.00
              Cat |          1       20.00       60.00
              Cat |          1       20.00       80.00
              Dog |          1       20.00      100.00
      ------------+-----------------------------------
            Total |          5      100.00
      If that isn't what you expect, use the recode command to create a new variable with two values, and in the process assign labels to those values.
      Code:
      . recode x (1 5=1 "Dog") (2/5=2 "Cat"), generate(xx)
      (3 differences between x and xx)
      
      . tab xx
      
      RECODE of x |      Freq.     Percent        Cum.
      ------------+-----------------------------------
              Dog |          2       40.00       40.00
              Cat |          3       60.00      100.00
      ------------+-----------------------------------
            Total |          5      100.00

      Comment


      • #4
        Clyde and William, thanks a lot.

        William, thank you. I am glad you suggested recoding, and that is what I have exactly done. It works. Thanks!

        Comment

        Working...
        X