Hi, SAS Users-
Me again. I'm trying to change a date (stored in two variables, one with a SAS date format and the other as numeric) to a string variable with a two-digit year and a three character month abbreviation--e.g., "16-Jan". Ultimately, I want to join the data I'm prepping with another file, which has months in this format. But instead of getting "16-Jan", I get "65-Jan": right month, but wrong year.
Here's the code I'm using:
data ucd5; set ucd4; highmme_rate = highmme/pop*100000; month = put(date2, monname3.); monyear2 = cats(put(year(date2), year2.),"-", put(date2, monname3.)); year2 = put(year(date2), year.); *format date yymon.; drop monyear; run;Here's some sample rows from the input data ("ucd4"):
As readily seen, the date variable ("date") maps correctly on to the number of days since Jan 1, 1960: 2016-01-01 is, in fact 20454 days from 1960-01-01. The above code gets the month right, but outputs the year as 1965. Here are some sample rows from the created dataset ("ucd5"):
Any ideas on what's going on? Since the code outputs 1965 as the year from both the "date" and the "date2" variables, I suspect that the origin date is something other than 1960-01-01. How can I check and, if appropriate, reset the origin date? Or is the problem something else?
Many thanks,
David
put(year(date2), year2.)
This is incorrect. You take the year (1965) then apply a year format to that date, which is why you're getting 1965
Instead try
put(date2, yymmdd2.) to get just the last two digits.
The year format would be applied to a date variable, not to the year value.
put(year(date2), year2.)
This is incorrect. You take the year (1965) then apply a year format to that date, which is why you're getting 1965
Instead try
put(date2, yymmdd2.) to get just the last two digits.
The year format would be applied to a date variable, not to the year value.
Many thanks, Reeza. Worked like a charm.
But I'm not sure why: where is the 1965 coming from? It's nowhere in the original data I'm trying to manipulate. Why would year(date2) return 1965 instead of 2016?
Best,
David
Update: for some reason, I reran my original code and it's now working fine. So, both the original code and your suggested replacement seem to work. Working with dates in SAS seems unnecessarily difficult! 🙂
data want;
date2=20545;
Actual_SAS_Date = date2;
Year_formatted_as_date = year(date2);
year_formatted_as_num= year(date2);
format Actual_SAS_Date date9. Year_formatted_as_date year. year_formatted_as_num 8.;
run;
proc print data=want;
run;
Results:
Thanks for this explanation, Reeza. I reran the original code and you're right: it wasn't working. I don't know why I thought it was.
Again, I tender my gratitude for your taking the time to respond.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.