I am trying to use the Year function to make a year variable using a date variable. Below is the excerpt of the code I'm using, along with the error message I am getting. I have broken it up into different data steps as I've tried a few different ways to get the Year function to work but haven't been able to get it to work. Could someone please provide some guidance?
Thanks!
CODE:
data retained;
set period;
where date >= '1Jan2010:0:0:0'dt;
keep date;
Data retained2;
set retained;
date1 = put(date,datetime7.);
Data retained3;
set retained2;
year = year(date1);
ERROR Message:
NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column).
41:17
NOTE: Invalid numeric data, date1='01JAN15' , at line 41 column 17.
The put function always renders character as its result. What you need is a SAS date value, which is a count of days from 1960-01-01:
data retained;
set period;
where date >= '1Jan2010:0:0:0'dt;
year = year(datepart(date));
keep date year;
run;
The datepart() function creates the SAS date value from the SAS datetime value.
Can you post a sample of your dataset period representing the values at best
And any reason to convert date to char date using put(date,datetime7.); ???
The put function always renders character as its result. What you need is a SAS date value, which is a count of days from 1960-01-01:
data retained;
set period;
where date >= '1Jan2010:0:0:0'dt;
year = year(datepart(date));
keep date year;
run;
The datepart() function creates the SAS date value from the SAS datetime value.
awesome, that works. Thanks so much!
Catch the best of SAS Innovate 2025 — anytime, anywhere. Stream powerful keynotes, real-world demos, and game-changing insights from the world’s leading data and AI minds.
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.