Announcement

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

  • Recoding to a categorical variable_'the number of new and transformed varnames should be the same r(198)'

    Hi there,
    I am trying to recode age in my dataset (min 40, max 70) into 7 categories;

    recode age 40/44 = 40-44 45/49 = 45-49 50/54 = 50-54 55/59 = 55-59 60/64 = 60
    > -64 65/69 = 65-69 70/74 = 70-74, generate(Age groups)

    OR

    recode age 40/44 = A 45/49 = B 50/54 = C 55/59 = D 60/64 = E 65/69 = F 70/74
    > = G, generate(Age groups)


    but then getting this error message;

    the number of new and transformed varnames should be the same
    r(198);

    Please assist?

    Thank you.



  • #2
    My quick glance at the help-file for recode suggests that your syntax is invalid. Also you appear to trying to make the new age group variable a string variable -- unnecessary. Try the following code (untested)

    Code:
    recode age (40/44 = 1) (45/49 = 2) (50/54 = 3)  (55/59 = 4)  (60/64 = 5)  ///
                  (65/69 = 6)   (70/74 = 7)  (else = missing), generate(agegp)
    label variable agegp "age, categorised"
    label define agegp 1 "40-44" 2 "45-49" 3 "50-54" 4 "55-59" 5 "60-64" ///
                  6 "65-69" 7 "70-74"
    label values agegp agegp
    tabulate agegp, missing
    Welcome to Statalist! Please take a moment to read through the Forum (black bar at top of page) and note the advice how to post effectively, including using 'CODE' delimiters

    Comment


    • #3
      Hi Stephen,
      Thank you for your reply - very useful. It worked!

      Comment


      • #4
        Now that you've read and understood Stephen's clear exposition of the recode and label values commands, let me show you that the recode command can also do the work of the label values command.
        Code:
        recode age (40/44 = 1 "40-44") (45/49 = 2 "45-49") (50/54 = 3 "50-54") ///
                   (55/59 = 4 "55-59") (60/64 = 5 "60-64") (65/69 = 6 "65-69") ///
                   (70/74 = 7 "70-74") (else = missing), generate(agegp)
        label variable agegp "age, categorised"
        tabulate agegp, missing
        As you can guess, recode will generate a value label using the recoded values and strings within each set of parentheses. Stephen's exposition of the label values command is important to keep in mind, because value labels are good for more than just recoded variables.

        This technique is actually particularly helpful when you have a recode command like
        Code:
        recode industry (1-50 = 1 "Manufacturing") ...
        because having the value labels included in the recode command helps remind you what it is you intend the recode to accomplish.

        Comment


        • #5
          Another way to code age into a categorical variable is by using the cond and inrange() functions, which I learnt from Nick Cox.
          Code:
          label def agegrp 1 "40-44" 2 "45-49" 3 "50-54" 4 "55-59" 5 "60-64" 6 "65-69" 7 "70-74"  
           
          foreach v in age { 
              gen `v'2 = cond(inrange(`v', 40, 44), 1,  ///
                         cond(inrange(`v', 45, 49), 2,  ///
                         cond(inrange(`v', 50, 54), 3,  ///
                         cond(inrange(`v', 55, 59), 4,  ///
                         cond(inrange(`v', 60, 64), 5,  ///
                         cond(inrange(`v', 65, 69), 6,  ///
                         cond(inrange(`v', 70, 74), 7, .)))))))
              label val `v'2 agegrp 
          } 
          
          tabulate age2, mi
          Remember to drop your code into the delimiters by using '#' on the toolbar. (Refer FAQs at the top of the page). For more on the cond function see https://www.stata-journal.com/sjpdf....iclenum=pr0016.

          Comment

          Working...
          X