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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 6962 views
  • 0 likes
  • 3 in conversation