BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Bluekeys49
Obsidian | Level 7

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
PaigeMiller
Diamond | Level 26

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

View solution in original post

8 REPLIES 8
PaigeMiller
Diamond | Level 26

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
Bluekeys49
Obsidian | Level 7

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.

PaigeMiller
Diamond | Level 26

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

--
Paige Miller
Bluekeys49
Obsidian | Level 7

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.  

Kurt_Bremser
Super User

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?

andreas_lds
Jade | Level 19

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

 

(Removed code, that Kurt already posted)

ballardw
Super User

@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.

Bluekeys49
Obsidian | Level 7

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.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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