## Calculating Person-Years by Age

Solved
Occasional Contributor
Posts: 5

# Calculating Person-Years by Age

Using 9.3. My data are in 1 case per row format, with case ID and three dates: dob (dob), entry into cohort(first_dt), exit from cohort(last_dt). For context, the study spanned 10 years, so typical rows would be:

`id dob        first_dt   last_dt1  05MAR1980  01JAN2005  31DEC2014 (alive throughout study)2  12AUG2006  12AUG2006  31DEC2014 (born during study)3  19SEP1975  01JAN2005  20MAY2011 (died or censored during study)`

I need the amount of time the cohort has spent in one year age increments. For instance, how many total person years spent at age less than 1, how many total PYs at age 1, at age 2, etc. over the course of the study?

I'm fairly certain there's a simple way to do this (perhaps a table that can be output in a survival analysis?) but after several hours of trying to find a simple solution, I'm stumped. Thanks for any help you can provide.

S

Accepted Solutions
Solution
‎04-28-2016 12:35 AM
Super User
Posts: 10,761

## Re: Calculating Person-Years by Age

I have no time to go through all of these . Just give you some code to guide you how to solve this question.

``````data _null_;
dob='05MAR1980'd;
first_dt='01JAN2005'd;
last_dt='31DEC2014'd;

x=yrdif(dob,first_dt,'act/act');
age=int(x);
x=ceil(x)-x;
put x 'at age' age;

do j=intck('year',dob,first_dt,'c')+1 to intck('year',dob,last_dt,'c')-1;
put '1 at age' j;
end;

x=yrdif(dob,last_dt,'act/act');
age=int(x);
x=mod(x,1);
put x 'at age' age;
run;``````

All Replies
Super User
Posts: 13,498

## Re: Calculating Person-Years by Age

Provide some example output. It seems as though you may be asking for multiple output variables.

And there appears to be a dependence on an unstated value: when the study began.

I am also not sure that anyone would ever have more than one person year at "age less than one", so there may be some other information not explicitly stated there as well such as you are summarizing after the "years" are calculated.

Super User
Posts: 23,663

## Re: Calculating Person-Years by Age

Occasional Contributor
Posts: 5

## Re: Calculating Person-Years by Age

Expected output would be

total_cohort_person_years_age_0= XX.X years

total_cohort_person_years_age_1 = XX.X years

total_cohort_person_years_age_2 = XX.X years

etc, to age 99

This can be done on a per row/case basis and then I can sum using proc sql, but I'm thinking there may be a simpler way to do this, as I said.

First date of study was 1/1/2005, but it shouldn't matter since I have entry(first_dt) in cohort and exit from(last_dt) cohort for each person.

Super User
Posts: 23,663

## Re: Calculating Person-Years by Age

Sorry, I don't understand your output. Ideally you should provide a sample input and sample output that has the calculations done so we're not assuming things.

Occasional Contributor
Posts: 5

## Re: Calculating Person-Years by Age

Person years (PY) at age X = time patient spent in study in years at a particular age point (age 1 year, age 2 years, etc.)

`id dob        first_dt   last_dt1  05MAR1980  01JAN2005  31DEC2014 (alive throughout study)2  12AUG2006  12AUG2006  31DEC2014 (born during study)3  19SEP1975  01JAN2005  20MAY2011 (died or censored during study)`

Case ID 1

Total of 10 PY in study. 24.83 years of age at first_dt.

Desired output:

.17 PY at age 24

1.00 PY at age 25

1.00 PY at age 26

1.00 PY at age 27

1.00 PY at age 28

1.00 PY at age 29

1.00 PY at age 30

1.00 PY at age 31

1.00 PY at age 32

1.00 PY at age 33

.83 PY at age 34

Case ID 2 was 0 at first_dt (her birth date). She spent 1.00 PY at age 0, 1.00 PY at age 1, 1.00 PY at age 2, 1.00 PY at age 3, 1.00 PY at age 4, 1.00 PY at age 5, 1.00 PY at age 6, 1.00 PY at age 7, and .39 PY at age 8. Total of 8.39 PY in study.

Case ID 3 was 29 at age of first_dt. She spent .71 PY at age 29, 1.00 PY at age 30, 1.00 PY at age 31, 1.00 PY at age 32, 1.00 PY at age 33, 1.00 PY at age 33, .67 PY at age 34. Total of 6.38 PY.

Does that clarify?

Solution
‎04-28-2016 12:35 AM
Super User
Posts: 10,761

## Re: Calculating Person-Years by Age

I have no time to go through all of these . Just give you some code to guide you how to solve this question.

``````data _null_;
dob='05MAR1980'd;
first_dt='01JAN2005'd;
last_dt='31DEC2014'd;

x=yrdif(dob,first_dt,'act/act');
age=int(x);
x=ceil(x)-x;
put x 'at age' age;

do j=intck('year',dob,first_dt,'c')+1 to intck('year',dob,last_dt,'c')-1;
put '1 at age' j;
end;

x=yrdif(dob,last_dt,'act/act');
age=int(x);
x=mod(x,1);
put x 'at age' age;
run;``````
Occasional Contributor
Posts: 5

## Re: Calculating Person-Years by Age

Wonderful! I needed the intck function, and didn't know it. Thanks for setting up for me so nicely.

Super User
Posts: 23,663

## Re: Calculating Person-Years by Age

Do you need the value for each year or just the total?

The total is relatively easily to calculate using the yrdif function, but if you want it for each age and the duration its a bit more work.

This should get you started though:

``````data have;
informat dob first_dt last_dt anydtdte.;
input id dob        first_dt   last_dt;
cards;
1  05MAR1980  01JAN2005  31DEC2014
2  12AUG2006  12AUG2006  31DEC2014
3  19SEP1975  01JAN2005  20MAY2011
;
run;

data want;
set have;
py=yrdif(first_dt, last_dt+1, 'act/act');
format py 8.2 first_dt last_dt dob date9.;
run;

proc print data=want;
run;``````
Occasional Contributor
Posts: 5

## Re: Calculating Person-Years by Age

Thanks - yes, I need it for each age, unfortunately. But I think we've solved it. I appreciate your time and input.

🔒 This topic is solved and locked.