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.
"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.;
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.
@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.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.