Announcement

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

  • String variable for age groups

    Dear Statalisters,

    I am using Stata 13.1 and I am trying to convert a variable for age groups in the form 1- ; 6- ; 11- , obtained after converting a continuous age variable (1-100) using the following command:
    Code:
    egen agegroup = cut(age), at(1 (5) 100) label
    into a string variable with specific age intervals that look like this 1-5 ; 6-10 ; 11-15 ; ...

    Any help will be appreciated.

    Thanks.

  • #2
    Well, first understand that agegroup is not a string variable. It is a numeric variable with value labels that make it display as 1-, 6-, etc. But the actual values of agegroup are 1, 2, 3, ...etc. While it isn't hard to write some code that will convert agegroup to the string variable you want, it is easier to do it directly:

    Code:
    gen int lowage = ceil(age/5)
    gen int highage = lowage + 4
    gen agegroup = string(lowage) + "-" + string(highage)

    Comment


    • #3
      Clyde Schechter gives excellent advice as always. I note a small typo in his code, corrected below.

      More crucially, what are you are going to do with this string variable? A little thought shows that it won't even sort in the right order. However, it does have a purpose. It can easily provide value labels for your age classes. Here I use labmask (SJ) to copy the values of the new string variable, so that they are seen whenever you tabulate or (usually) graph using that variable.

      Code:
      . clear
      
      . set obs 100
      number of observations (_N) was 0, now 100
      
      . gen age = _n
      
      . gen int lowage = 5 * ceil(age/5)
      
      . gen int highage = lowage + 4
      
      . gen agegroup = string(lowage) + "-" + string(highage)
      
      . tab agegroup
      
         agegroup |      Freq.     Percent        Cum.
      ------------+-----------------------------------
            10-14 |          5        5.00        5.00
          100-104 |          5        5.00       10.00
            15-19 |          5        5.00       15.00
            20-24 |          5        5.00       20.00
            25-29 |          5        5.00       25.00
            30-34 |          5        5.00       30.00
            35-39 |          5        5.00       35.00
            40-44 |          5        5.00       40.00
            45-49 |          5        5.00       45.00
              5-9 |          5        5.00       50.00
            50-54 |          5        5.00       55.00
            55-59 |          5        5.00       60.00
            60-64 |          5        5.00       65.00
            65-69 |          5        5.00       70.00
            70-74 |          5        5.00       75.00
            75-79 |          5        5.00       80.00
            80-84 |          5        5.00       85.00
            85-89 |          5        5.00       90.00
            90-94 |          5        5.00       95.00
            95-99 |          5        5.00      100.00
      ------------+-----------------------------------
            Total |        100      100.00
      
      labmask lowage, values(agegroup)
      
      tab lowage
      
           lowage |      Freq.     Percent        Cum.
      ------------+-----------------------------------
              5-9 |          5        5.00        5.00
            10-14 |          5        5.00       10.00
            15-19 |          5        5.00       15.00
            20-24 |          5        5.00       20.00
            25-29 |          5        5.00       25.00
            30-34 |          5        5.00       30.00
            35-39 |          5        5.00       35.00
            40-44 |          5        5.00       40.00
            45-49 |          5        5.00       45.00
            50-54 |          5        5.00       50.00
            55-59 |          5        5.00       55.00
            60-64 |          5        5.00       60.00
            65-69 |          5        5.00       65.00
            70-74 |          5        5.00       70.00
            75-79 |          5        5.00       75.00
            80-84 |          5        5.00       80.00
            85-89 |          5        5.00       85.00
            90-94 |          5        5.00       90.00
            95-99 |          5        5.00       95.00
          100-104 |          5        5.00      100.00
      ------------+-----------------------------------
            Total |        100      100.00

      Comment


      • #4
        Many thanks Clyde and Nick. Thanks Nick for the extra tip on sorting age groups. Best regards.

        Comment


        • #5
          Actually, there is another bug in this code!

          Code:
          . clear 
          
          . set obs 101 
          number of observations (_N) was 0, now 101
          
          . gen age = _n-1 
          
          . gen int lowage = 5 * floor(age/5)
          
          . gen int highage = lowage + 4
          
          . gen agegroup = string(lowage) + "-" + string(highage)
          
          . tab agegroup 
          
             agegroup |      Freq.     Percent        Cum.
          ------------+-----------------------------------
                  0-4 |          5        4.95        4.95
                10-14 |          5        4.95        9.90
              100-104 |          1        0.99       10.89
                15-19 |          5        4.95       15.84
                20-24 |          5        4.95       20.79
                25-29 |          5        4.95       25.74
                30-34 |          5        4.95       30.69
                35-39 |          5        4.95       35.64
                40-44 |          5        4.95       40.59
                45-49 |          5        4.95       45.54
                  5-9 |          5        4.95       50.50
                50-54 |          5        4.95       55.45
                55-59 |          5        4.95       60.40
                60-64 |          5        4.95       65.35
                65-69 |          5        4.95       70.30
                70-74 |          5        4.95       75.25
                75-79 |          5        4.95       80.20
                80-84 |          5        4.95       85.15
                85-89 |          5        4.95       90.10
                90-94 |          5        4.95       95.05
                95-99 |          5        4.95      100.00
          ------------+-----------------------------------
                Total |        101      100.00
          
          . labmask lowage, values(agegroup) 
          
          . tab lowage 
          
               lowage |      Freq.     Percent        Cum.
          ------------+-----------------------------------
                  0-4 |          5        4.95        4.95
                  5-9 |          5        4.95        9.90
                10-14 |          5        4.95       14.85
                15-19 |          5        4.95       19.80
                20-24 |          5        4.95       24.75
                25-29 |          5        4.95       29.70
                30-34 |          5        4.95       34.65
                35-39 |          5        4.95       39.60
                40-44 |          5        4.95       44.55
                45-49 |          5        4.95       49.50
                50-54 |          5        4.95       54.46
                55-59 |          5        4.95       59.41
                60-64 |          5        4.95       64.36
                65-69 |          5        4.95       69.31
                70-74 |          5        4.95       74.26
                75-79 |          5        4.95       79.21
                80-84 |          5        4.95       84.16
                85-89 |          5        4.95       89.11
                90-94 |          5        4.95       94.06
                95-99 |          5        4.95       99.01
              100-104 |          1        0.99      100.00
          ------------+-----------------------------------
                Total |        101      100.00

          Comment


          • #6
            Yes, thanks for that correction, Nick.

            Comment


            • #7
              One more minor correction is that the line for the lowage should read as follows so that each age falls into the corresponding age interval:

              Code:
              gen int lowage = 5 * ceil(age/5)-4
              Thanks again for your help..

              Comment


              • #8
                Imed Limam

                I don't know which post you are correcting, but I don't understand that suggestion.

                It maps 0 to -4 and 100 to 96.

                Further, see this test of the code in #5. I use groups (SSC).

                Code:
                . groups age lowage
                
                  +---------------------------------+
                  | age    lowage   Freq.   Percent |
                  |---------------------------------|
                  |   0       0-4       1      0.99 |
                  |   1       0-4       1      0.99 |
                  |   2       0-4       1      0.99 |
                  |   3       0-4       1      0.99 |
                  |   4       0-4       1      0.99 |
                  |---------------------------------|
                  |   5       5-9       1      0.99 |
                  |   6       5-9       1      0.99 |
                  |   7       5-9       1      0.99 |
                  |   8       5-9       1      0.99 |
                  |   9       5-9       1      0.99 |
                  |---------------------------------|
                  |  10     10-14       1      0.99 |
                  |  11     10-14       1      0.99 |
                  |  12     10-14       1      0.99 |
                  |  13     10-14       1      0.99 |
                  |  14     10-14       1      0.99 |
                  |---------------------------------|
                  |  15     15-19       1      0.99 |
                  |  16     15-19       1      0.99 |
                  |  17     15-19       1      0.99 |
                  |  18     15-19       1      0.99 |
                  |  19     15-19       1      0.99 |
                  |---------------------------------|
                  |  20     20-24       1      0.99 |
                  |  21     20-24       1      0.99 |
                  |  22     20-24       1      0.99 |
                  |  23     20-24       1      0.99 |
                  |  24     20-24       1      0.99 |
                  |---------------------------------|
                  |  25     25-29       1      0.99 |
                  |  26     25-29       1      0.99 |
                  |  27     25-29       1      0.99 |
                  |  28     25-29       1      0.99 |
                  |  29     25-29       1      0.99 |
                  |---------------------------------|
                  |  30     30-34       1      0.99 |
                  |  31     30-34       1      0.99 |
                  |  32     30-34       1      0.99 |
                  |  33     30-34       1      0.99 |
                  |  34     30-34       1      0.99 |
                  |---------------------------------|
                  |  35     35-39       1      0.99 |
                  |  36     35-39       1      0.99 |
                  |  37     35-39       1      0.99 |
                  |  38     35-39       1      0.99 |
                  |  39     35-39       1      0.99 |
                  |---------------------------------|
                  |  40     40-44       1      0.99 |
                  |  41     40-44       1      0.99 |
                  |  42     40-44       1      0.99 |
                  |  43     40-44       1      0.99 |
                  |  44     40-44       1      0.99 |
                  |---------------------------------|
                  |  45     45-49       1      0.99 |
                  |  46     45-49       1      0.99 |
                  |  47     45-49       1      0.99 |
                  |  48     45-49       1      0.99 |
                  |  49     45-49       1      0.99 |
                  |---------------------------------|
                  |  50     50-54       1      0.99 |
                  |  51     50-54       1      0.99 |
                  |  52     50-54       1      0.99 |
                  |  53     50-54       1      0.99 |
                  |  54     50-54       1      0.99 |
                  |---------------------------------|
                  |  55     55-59       1      0.99 |
                  |  56     55-59       1      0.99 |
                  |  57     55-59       1      0.99 |
                  |  58     55-59       1      0.99 |
                  |  59     55-59       1      0.99 |
                  |---------------------------------|
                  |  60     60-64       1      0.99 |
                  |  61     60-64       1      0.99 |
                  |  62     60-64       1      0.99 |
                  |  63     60-64       1      0.99 |
                  |  64     60-64       1      0.99 |
                  |---------------------------------|
                  |  65     65-69       1      0.99 |
                  |  66     65-69       1      0.99 |
                  |  67     65-69       1      0.99 |
                  |  68     65-69       1      0.99 |
                  |  69     65-69       1      0.99 |
                  |---------------------------------|
                  |  70     70-74       1      0.99 |
                  |  71     70-74       1      0.99 |
                  |  72     70-74       1      0.99 |
                  |  73     70-74       1      0.99 |
                  |  74     70-74       1      0.99 |
                  |---------------------------------|
                  |  75     75-79       1      0.99 |
                  |  76     75-79       1      0.99 |
                  |  77     75-79       1      0.99 |
                  |  78     75-79       1      0.99 |
                  |  79     75-79       1      0.99 |
                  |---------------------------------|
                  |  80     80-84       1      0.99 |
                  |  81     80-84       1      0.99 |
                  |  82     80-84       1      0.99 |
                  |  83     80-84       1      0.99 |
                  |  84     80-84       1      0.99 |
                  |---------------------------------|
                  |  85     85-89       1      0.99 |
                  |  86     85-89       1      0.99 |
                  |  87     85-89       1      0.99 |
                  |  88     85-89       1      0.99 |
                  |  89     85-89       1      0.99 |
                  |---------------------------------|
                  |  90     90-94       1      0.99 |
                  |  91     90-94       1      0.99 |
                  |  92     90-94       1      0.99 |
                  |  93     90-94       1      0.99 |
                  |  94     90-94       1      0.99 |
                  |---------------------------------|
                  |  95     95-99       1      0.99 |
                  |  96     95-99       1      0.99 |
                  |  97     95-99       1      0.99 |
                  |  98     95-99       1      0.99 |
                  |  99     95-99       1      0.99 |
                  |---------------------------------|
                  | 100   100-104       1      0.99 |
                  +---------------------------------+

                Comment


                • #9
                  Nick here is the code I was referring to:

                  Code:
                  clear
                  set obs 100
                  gen age = _n
                  gen int lowage = 5 * ceil(age/5)-4
                  gen int highage = lowage + 4
                  gen agegroup = string(lowage) + "-" + string(highage)
                  tab agegroup
                  labmask lowage, values(agegroup)
                  tab lowage

                  Comment


                  • #10
                    Thanks for the detail. If you know that no one is below 1, then you're fine with that too.

                    Comment


                    • #11
                      Thanks Nick.

                      Comment

                      Working...
                      X