BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
KenH
Calcite | Level 5

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.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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.

View solution in original post

3 REPLIES 3
novinosrin
Tourmaline | Level 20

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.); ???

Kurt_Bremser
Super User

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.

KenH
Calcite | Level 5

awesome, that works.  Thanks so much!

SAS Innovate 2025: Register Today!

 

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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
  • 3 replies
  • 7604 views
  • 0 likes
  • 3 in conversation