Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: how do i increment a counter

From   Christian Holz <>
Subject   Re: st: how do i increment a counter
Date   Wed, 31 Aug 2005 22:40:45 +0100

aine dooley wrote:
> I'm sure there is a more elegant way to do this, however ...

indeed, there is. Any a much easier and faster (both typing and computing time) way!

egen age1_sum=sum(exposure_time) if age<=50
egen age2_sum=sum(exposure_time) if age>50 & age <=75
egen age3_sum=sum(exposure_time) if age>75 & age <=90
egen age4_sum=sum(exposure_time) if age>90 & !mi(age)

creates a new variable for every "age group" which contains the cummulative exposure_time of that group if the patient belongs to that group and is missing otherwise, whereas

recode age (min/50=1) (51/75=2) (76/90=3) (90/max=4), gen (age_c)
bysort age_c: egen age_sum=sum(exposure_time)

creates one variable age_sum that contains the cummulative exposure_time of each patient's age group. (it works on the assumption that your age variable is integer)

Best from Scotland,

Christian Holz
Department of Sociology
University of Glasgow
Scotland, U.K.
* For searches and help try:

© Copyright 1996–2017 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index