1

I am trying to count the number of times an event occurred before a particular age. I have data that shows the age at each event over a lifetime (age_at_event1-age_at_event3), as well as the age at which I am no longer interested in counting such events (stop_age). I would like to create a variable (sum_event) which counts the number of times the event of interest occurred prior to the stop age. An example:

ID    age_event1    age_event2    age_event3    stop_age  sum_event
 1         10          17           45             34        2         
 2         23          31           32             54        3   
 3         25          55            .             32        1    
 4         21           .            .             22        1

How do I create the appropriate sum_event variable?

Julia
  • 37
  • 1
  • 2
  • 5

1 Answers1

1

If you don't want to reshape your data, then you can loop over variables and count:

clear
set more off

*----- example data -----

input ///
ID    age_event1    age_event2    age_event3    stop_age  sum_event
 1         10          17           45             34        2         
 2         23          31           32             54        3   
 3         25          55            .             32        1    
 4         21           .            .             22        1
end

list

*----- what you want -----

gen sumevent2 = 0
foreach var of varlist age_event1 age_event2 age_event3 {
    replace sumevent2 = sumevent2 + (`var' < stop_age)
}

list

For numbered variables that follow some pattern, you can try something like:

<snip>

gen sumevent2 = 0
forvalues i = 1/3 {
    replace sumevent2 = sumevent2 + (age_event`i' < stop_age)
}

Another way with reshape:

*----- what you want -----

<snip>

reshape long age_event, i(ID) j(j)
bysort ID: egen sumevent2 = total(age_event < stop_age)
reshape wide // if you really need to go back to wide

list
Roberto Ferrer
  • 11,024
  • 1
  • 21
  • 23
  • Worked great. Thank you. Any chance you could tell me how to avoid having to type out the age_event variables one by one? Despite my example I actually have dozens of age_event variables. I know the forval command comes into play, but I'm not sure of the specifics. – Julia Nov 11 '14 at 01:47
  • Try `age_event*` but make sure you really want to include all variables that start in this way. I also added the `forvalues` solution to the answer. – Roberto Ferrer Nov 11 '14 at 02:28
  • http://www.stata-journal.com/sjpdf.html?articlenum=pr0046 reviews technique here. – Nick Cox Nov 11 '14 at 09:53