- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.); ???
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
awesome, that works. Thanks so much!