Announcement

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

  • Condition command stata

    I want to create a new variable (ageT) with the -cond- programming function. Is there any way I efficiently code my repetitive example below?

    Code:
    set obs 100
    gen agegrp = runiformint(1,5)
    
    gen age1 = cond(agegrp==1,0,.)
    gen age2 = cond(agegrp==2,2,.)
    gen age3 = cond(agegrp==3,4,.)
    gen age4 = cond(agegrp==4,6,.)
    gen age5 = cond(agegrp==5,8,.)
    egen ageT = rowmax(age1 age2 age3 age4 age5)
    Thanks

  • #2
    Your desired variable can be generated in a single line, taking advantage of the fact that (0,2,4,6,8) is a simple arithmetic sequence.

    Code:
    gen ageT = 2*(agegrp-1)
    Last edited by Ali Atia; 17 May 2022, 21:05.

    Comment

    Working...
    X