Announcement

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

  • Generating agegroups with a Loop

    Dear all,
    I have been trying to find a way to use 'foreach' or 'forvalues' loop to generate agegroups. I have a survey dataset including and individual age variable. Now I would like to create agegroups in steps of 4 (<1, 1-4, 5-9, 10-14, ....., 95-99, >100). The way I am able to do it is continuing as shown below, but as there are more tasks like this I need to do in my analysis it would be very helpful to be able to do it a shorter (and less error prone) way. I tried to use 'forvalues' and give the ages in steps, but I cannot find a solution yet.
    Eventually I will need to attach a specific growth rate to each age group which I will try to do in a further loop, or, if possible, in the same one. (That is if it is possible to attach different values to each agegroup in a loop, I imagine I would need to enter each value and then the loop would be redundant.)
    Any help or suggestion would be very appreciated!
    Thank you very much!

    gen agegroup=.
    replace agegroup=1 if age<1
    replace agegroup=2 if age>=1 & age<=4
    replace agegroup=3 if age>=5 & age<=9
    replace agegroup=4 if age>=10 & age<=14
    replace agegroup=5 if age>=15 & age<=19
    ...

    forvalues age = 1(1)4 5(1)9 10(1)14 15(1)19 20(1)24 25(1)29 30(1)34 35(1)39 {
    gen agegroup
    }

  • #2
    See

    Code:
    help recode
    help irecode
    Best
    Daniel

    Comment


    • #3
      No matter how you do it, you have to give Stata the class limits. Here are two more ways:

      Code:
      gen agegroup = . 
      
      numlist "1 4(5)99"
      tokenize `r(numlist)'  
      
      forval j = 1/21 { 
           replace agegroup = `j' if age <= ``j'' & missing(agegroup) 
      } 
      
      replace agegroup = 22 if missing(agegroup) 
      
      gen agegroup2 = cond(age <= 1, 1, 1 + floor((age + 1)/5)) 
      replace agegroup2 = 22 if missing(agegroup2)




      Comment


      • #4
        Thank you so much! That is great, really helpful!

        Comment


        • #5
          We can improve both of those:

          Code:
          gen agegroup = 22 if age >= 100 & age < .  
          numlist "1 4(5)99"
          tokenize `r(numlist)'    
          forval j = 21(-1)1 {      
               replace agegroup = `j' if age <= ``j''  
          }    
          
          gen agegroup2 = cond(age <= 1, 1, 1 + floor((age + 1)/5))  
          replace agegroup2 = 22 if age >= 100 & age < .

          Comment

          Working...
          X