Calculating age at diagnosis from PNR and diagnosis date

It is often necessary to calculate age at diagnosis from variables representing the personal identification number and the date of diagnosis (stored as a character variable).

The first step is to create SAS date variables representing the birth date and diagnosis date. A SAS date variable stores dates as the number of days between January 1, 1960 and the date.

The variable DXDAT is a character variable, which is converted to a date variable called DIDAT2 using the input function. The SAS date variable called BIRTH is constructed in similar fashion, except we must first extract the substring (using the SUBSTR function) of the personal identification number which represents the date of birth.

Age at diagnosis is then calculated as the number of completed years between the two dates using SAS code described here.

This code assumes that all dates are in the 1900s.

The SAS code can be downloaded here.

data temp;
length pnr $ 11 dxdat $ 6;
input pnr dxdat;

/* convert the character variable
to a SAS date variable */

/* extract the birthdate from PNR */

- (day(didat2) < day(birth))) / 12);

format didat2 birth date.;

96511289999 990622
93404199999 590420

proc print data=temp;
title 'Calculating age at diagnosis';
var pnr dxdat birth didat2 age_dx;

The output from this code is as follows:


96511289999  990622  28NOV65  22JUN99    33
93404199999  590420  19APR34  20APR59    25