** This example provides SAS code for calculating "person years" in a cohort study ; ** For each age group, age at the start and age at the end must be calculated. ; * counting Person Years by year and age group; proc format value age5f * 5 year age group format; 20-<25='01' 25-<30='02' 30-<35='03' 35-<40='04' 40-<45='05' 45-<50='06' 50-<55='07' 55-<60='08' 60-<65='09' 65-<70='10' 70-<75='11' 75-<80='12' 80-<85='13' 85-<90='14' 90-<95='15' 95-high='16'; * format to identify minimum age value for age group. Note that this could probably be done mathematically as well; value agesrtf 01='20' 02='25' 03='30' 04='35' 05='40' 06='45' 07='50' 08='55' 09='60' 10='65' 11='70' 12='75' 13='80' 14='85' 15='90' 16='95' ; * age group labels; value age5l 01='20-<25' 02='25-<30' 03='30-<35' 04='35-<40' 05='40-<45' 06='45-<50' 07='50-<55' 08='55-<60' 09='60-<65' 10='65-<70' 11='70-<75' 12='75-<80' 13='80-<85' 14='85-<90' 15='90-<95' 16='95+' ; run; data pyears; set temp; * partition person years by year (calendar), and age group; * sdate=study start date (SAS date); * edate=study end date (SAS date)= minumum of date of death, date of event, date of loss to follow-up or end of study; * bdate=birth date (SAS date); do year=year(sdate) to year(edate); * calculate date at start of year (sdate_year) and date at end of year (edate_year); sdate_year=max(sdate,mdy(1,1,year)); edate_year=min(edate,mdy(1,1,year+1)); format sdate_year edate_year yymmddn8.; * calculate age at start date (sage) and age at end date (eage); sage= yrdif(bdate, sdate_year, 'ACT/ACT'); eage= yrdif(bdate, edate_year, 'ACT/ACT'); * ensure that person years are distributed to each age group appropriately; do agegrp=input(put(sage,age5f.),2.) to input(put(eage,age5f.),2.); * Be careful about the last age group (95+). If you have people older than 100 years in your cohort, their person years past age 100 will not be counted by this method. You will have to adjust age_end in this case.; age_end=min(eage,input(put(agegrp,agesrtf.),3.)+5); age_start=max(sage,input(put(agegrp,agesrtf.),2.)); pyear=age_end-age_start; if pyear> then output; end; format bdate sdate edate yymmddn8.; format agegrp age5l.; end; run;