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

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"):  

dbcrow_0-1681421480373.png

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"):  

dbcrow_1-1681421669440.png

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
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. 

View solution in original post

5 REPLIES 5
Reeza
Super User
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. 

dbcrow
Obsidian | Level 7

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! 🙂

Reeza
Super User
I wouldn't expect your original code to work at all.

So when you take the year of the date, it returns a numeric value 2016. SAS uses that to count the number of days from January 1, 1960. 2016 days from January 1, 1960 ends up somewhere in the year 1965 (2016/365.25 ~5.5).

The year format needs to be applied to a variable with a date format. It takes 1965 to be the date in 1965, not your original date.
Reeza
Super User
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:

Reeza_0-1681423367074.png

 

dbcrow
Obsidian | Level 7

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. 

SAS Innovate 2025: Register Now

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!

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
  • 5 replies
  • 920 views
  • 3 likes
  • 2 in conversation