BookmarkSubscribeRSS Feed
klongway
Calcite | Level 5
My birth date data is coded as two variables in the data set: birthmonth and birthyear. I'm struggling to combine these into a format that I can then subtract from the date the interviews were completed.

How would you suggest combining birthmonth and birthyear into birth date?


Thank you!!!
8 REPLIES 8
Reeza
Super User
Use the MDY() function to create a date.

Birth_Date = MDY(birthmonth, 15, birthyear);

Duration = interview_date - birth_date;
ChrisNZ
Tourmaline | Level 20

What are these two fields (birthmonth and birthyear) stored as? January and 1990? Are they strings?

You really need to help yourself by providing all the information when asking a question.

klongway
Calcite | Level 5
When I do your solution Reeza I end up with numbers in the birth date column.

So for participant 45, their birthdate is January 1978. When I use birth_date=MDY (birthmonth, 15, birthyear) I end up with a birthdate of 6589.

I'm sure its something simple I'm missing.
Patrick
Opal | Level 21

"January 1978" is not an exact date. @Reeza used 15 as the day of the month in the mdy() function to create such an exact date.

The mdy() function creates a SAS date value which is the count of days since 1/1/1960. To print such a count in a human readable form apply a format to the variable like: format Birth_Date date9.; 

Reeza
Super User
SAS dates are stored as numbers from the date of January 1 1960. All systems typically store dates in this fashion and then apply a format to show them displayed.

Add the following line to have them show as a SAS date.

format birth_date date9.;

hashman
Ammonite | Level 13

@klongway:

Nothing is missing, really. SAS date is the number of days since the beginning of 1960, which is 6589 for 1978-01-15. Since the SAS date is merely a number, SAS prints it by default using the BEST12. format, and so it is displayed as 6589. If you want to see it as a formatted date, you need to apply one of (a great variety of) date formats to your birth_date variable.  For example, run this:

data _null_ ;                                                                                                                           
  retain birthmonth 1 birthday 15 birthyear 1978 ;                                                                                      
  birth_date = MDY (birthmonth, birthday, birthyear) ;                                                                                           
  put birth_date=best12. birth_date=yymmdd10. ;                                                                                                             
run ;  

Result in the SAS log:

birth_date=6589 birth_date=1978-01-15 

If you're creating a data set with the birth_date variable and want it to appear formatted in a viewer or other kind of output medium, assign the format to the variable:

data want ;                                                                                                                             
  retain birthmonth 1 birthday 15 birthyear 1978 ;                                                                                      
  birth_date = MDY (birthmonth, birthday, birthyear) ;                                                                                  
  format birth_date yymmdd10. ;                                                                                                         
run ; 

I strongly recommend that a format where year, month, day are listed in this sequence be used, without exception, to preserve sanity when browsing data sorted by date.

 

Kind regards

Paul D. 

Kurt_Bremser
Super User

@hashman wrote:

I strongly recommend that a format where year, month, day are listed in this sequence be used, without exception, to preserve sanity when browsing data sorted by date.

 

Absolutely concur with this. There is an international standard for writing out dates and times (ISO 8601), and the SAS formats YYMMDDD10. and E8601DA10. both display a date according to this standard, in a nice human-readable way.

ChrisNZ
Tourmaline | Level 20

> to preserve sanity

I couldn't agree more.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 861 views
  • 8 likes
  • 6 in conversation