Obsidian | Level 7

## Calculating age using birthday that is numeric format (default 8.) and date of last visit

I have one variable called date_of_birth that is a numeric value with default length 8.  A couple of the values are 82902 (which is August 29, 2002) and 111797 (which is November 17, 1997).  I'll be creating a second variable, date_last_visit, in which I'll need to calculate age at time of date_last_visit.  Date_last_visit will be July 4, 2022 for everyone.  Any ideas how to code this?  I've started by trying to format the date_of_birth variable by coding DOB=put(date_of_birth,mmddyy8.) with no success.  Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
Diamond | Level 26

## Re: Calculating age using birthday that is numeric format (default 8.) and date of last visit

In order for any of this to work, you must use valid SAS date values, which are the number of days since 01JAN1960. Most humans could not tell you how many days since 01JAN1960 until (for example), 12APR2022. Fortunately, SAS provides many tools to do the translation from 12APR2022 to a valid SAS date which is 22747, provided your "dates" are in some meaningful and recognizable form.

Example: convert your "dates" into valid SAS dates, using the INPUT statement and the proper informat.

``````data want;
z='82902';
zn=input(z,mmddyy.);
run;``````

Once you have valid SAS dates, then computing age is done via the YRDIF function and using the date literal '04JUL2022'd to represent July 4, 2022.

``````data want2;
z='82902';
zn=input(z,mmddyy.);
age=yrdif(zn,'04JUL2022'd,'age');
run;``` ```

Adding: if your original "dates" are numeric, then you would need to do this in order to get the input statement with the informat to work.

``````data want2;
z=82902;
zn=input(put(z,6.),mmddyy.);
age=yrdif(zn,'04JUL2022'd,'age');
run;```  ```

@Bluekeys49 wrote:

I've started by trying to format the date_of_birth variable by coding DOB=put(date_of_birth,mmddyy8.) with no success.

This is NOT a format issue. Format is irrelevant. The problem is that your dates must be valid SAS dates, as explained earlier.

--
Paige Miller
8 REPLIES 8
Diamond | Level 26

## Re: Calculating age using birthday that is numeric format (default 8.) and date of last visit

In order for any of this to work, you must use valid SAS date values, which are the number of days since 01JAN1960. Most humans could not tell you how many days since 01JAN1960 until (for example), 12APR2022. Fortunately, SAS provides many tools to do the translation from 12APR2022 to a valid SAS date which is 22747, provided your "dates" are in some meaningful and recognizable form.

Example: convert your "dates" into valid SAS dates, using the INPUT statement and the proper informat.

``````data want;
z='82902';
zn=input(z,mmddyy.);
run;``````

Once you have valid SAS dates, then computing age is done via the YRDIF function and using the date literal '04JUL2022'd to represent July 4, 2022.

``````data want2;
z='82902';
zn=input(z,mmddyy.);
age=yrdif(zn,'04JUL2022'd,'age');
run;``` ```

Adding: if your original "dates" are numeric, then you would need to do this in order to get the input statement with the informat to work.

``````data want2;
z=82902;
zn=input(put(z,6.),mmddyy.);
age=yrdif(zn,'04JUL2022'd,'age');
run;```  ```

@Bluekeys49 wrote:

I've started by trying to format the date_of_birth variable by coding DOB=put(date_of_birth,mmddyy8.) with no success.

This is NOT a format issue. Format is irrelevant. The problem is that your dates must be valid SAS dates, as explained earlier.

--
Paige Miller
Obsidian | Level 7

## Re: Calculating age using birthday that is numeric format (default 8.) and date of last visit

With much trial and error, my difficulty is that I'm having trouble getting that Date_of_Birth variable to be valid SAS dates.  I've coded the following:

data want;
set have;
DOB = input(Date_of_Birth,mmddyy8.);
run;

I've also tried mmddyy. and mmddyy6. at the end of the above statement.  The new variable DOB comes back being blank for each record.

Diamond | Level 26

## Re: Calculating age using birthday that is numeric format (default 8.) and date of last visit

I gave an example of code where numeric date of birth 82902 is converted to a valid SAS date.

--
Paige Miller
Obsidian | Level 7

## Re: Calculating age using birthday that is numeric format (default 8.) and date of last visit

Thank you very much!  It worked.  I had missed that critical input(put(z,6.) piece which I had never used together before.  Many thanks.

Super User

## Re: Calculating age using birthday that is numeric format (default 8.) and date of last visit

See how to convert this hare-brained number to a SAS date:

``````data dates;
input date_of_birth;
datalines;
82902
111797
;

data want;
set dates;
length string \$8;
date_of_birth = input(put(date_of_birth,z6.),mmddyy6.);
format date_of_birth yymmdd10.;
run;``````

Hare-brained because such a number is useless in the first place, and anybody who still uses 2-digit years after Y2K needs to have their heads examined for terminal brain-rot.

Is this number the consequence of using PROC IMPORT? If yes, from what type of source data did you import?

## Re: Calculating age using birthday that is numeric format (default 8.) and date of last visit

There are leading zeros for the first nine days? December 1, 2000 is 120100, right?

(Removed code, that Kurt already posted)

Super User

## Re: Calculating age using birthday that is numeric format (default 8.) and date of last visit

@Bluekeys49 wrote:

I have one variable called date_of_birth that is a numeric value with default length 8.  A couple of the values are 82902 (which is August 29, 2002) and 111797 (which is November 17, 1997).  I'll be creating a second variable, date_last_visit, in which I'll need to calculate age at time of date_last_visit.  Date_last_visit will be July 4, 2022 for everyone.  Any ideas how to code this?  I've started by trying to format the date_of_birth variable by coding DOB=put(date_of_birth,mmddyy8.) with no success.  Thanks!

If you have any feedback ability to the source of this garbage less-than-optimal-value tell them that for clarity and consistency sake that months and days of year should always be 2 digits and years should be 4 digits.

Date formats, such as mmddyy, only come close to working when the actual numeric value of the date is a SAS created date value which means the number underlying is the number of days from 1 Jan 1960. While you can apply the format to any random sequence of digits, like 82902, the formatted value is likely seldom going to be what you want. Also PUT function always creates character values so would not have helped with use in any of the functions that manipulate date values.

https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/... has a PDF with much information about dates.

Obsidian | Level 7

## Re: Calculating age using birthday that is numeric format (default 8.) and date of last visit

Totally agree that these numeric input date values are not ideal at all.  Will definitely give feedback that year value should be 4 digits and not 2.

Discussion stats
• 8 replies
• 1304 views
• 2 likes
• 5 in conversation