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

Hi,

 

I have a column like this

 

03JUN2016:00:00:00
16OCT2014:00:00:00
02MAY2016:00:00:00
12JAN2011:00:00:00
30MAR2017:00:00:00
20SEP2017:00:00:00
08MAR2016:00:00:00
13JUL2018:00:00:00
02FEB2011:00:00:00
26MAR2015:00:00:00

 

it is date variable.

date.PNG

 

I use codes to extract the year from it

 

PROC SQL NOEXEC;
   SELECT (YEAR(DATE(t1.DATO))) AS CALCULATION
      FROM WORK.RANDRANDOMSAMPLEQUERY_FOR_DV_ t1;
QUIT;

What I got is:

2019
2019
2019
2019
2019
2019
2019
2019
2019
2019

 

which is obviously not right. Why???

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

correction

 

(YEAR(DATEPART(t1.DATO))) 

 

 

Bascially datepart function extracts the sas date value from the sas datetime value. And year function takes effect on the sas date value which is the number of days from Jan1,1960

 

Test

 

data have;
input datetime :datetime20.;
format datetime datetime20.;
cards;
03JUN2016:00:00:00
16OCT2014:00:00:00
02MAY2016:00:00:00
12JAN2011:00:00:00
30MAR2017:00:00:00
20SEP2017:00:00:00
08MAR2016:00:00:00
13JUL2018:00:00:00
02FEB2011:00:00:00
26MAR2015:00:00:00
;

data want;
set have;
year=year(datepart(datetime));
run;

Or

proc sql;
create table want as
select *,year(datepart(datetime)) as year
from have;
quit;

 

Results

datetime year
03JUN2016:00:00:00 2016
16OCT2014:00:00:00 2014
02MAY2016:00:00:00 2016
12JAN2011:00:00:00 2011
30MAR2017:00:00:00 2017
20SEP2017:00:00:00 2017
08MAR2016:00:00:00 2016
13JUL2018:00:00:00 2018
02FEB2011:00:00:00 2011
26MAR2015:00:00:00 2015

View solution in original post

3 REPLIES 3
novinosrin
Tourmaline | Level 20

correction

 

(YEAR(DATEPART(t1.DATO))) 

 

 

Bascially datepart function extracts the sas date value from the sas datetime value. And year function takes effect on the sas date value which is the number of days from Jan1,1960

 

Test

 

data have;
input datetime :datetime20.;
format datetime datetime20.;
cards;
03JUN2016:00:00:00
16OCT2014:00:00:00
02MAY2016:00:00:00
12JAN2011:00:00:00
30MAR2017:00:00:00
20SEP2017:00:00:00
08MAR2016:00:00:00
13JUL2018:00:00:00
02FEB2011:00:00:00
26MAR2015:00:00:00
;

data want;
set have;
year=year(datepart(datetime));
run;

Or

proc sql;
create table want as
select *,year(datepart(datetime)) as year
from have;
quit;

 

Results

datetime year
03JUN2016:00:00:00 2016
16OCT2014:00:00:00 2014
02MAY2016:00:00:00 2016
12JAN2011:00:00:00 2011
30MAR2017:00:00:00 2017
20SEP2017:00:00:00 2017
08MAR2016:00:00:00 2016
13JUL2018:00:00:00 2018
02FEB2011:00:00:00 2011
26MAR2015:00:00:00 2015
Reeza
Super User
The YEAR() function requires a DATE variable, not a DATETIME variable. You have a DATETIME variable. So you first need to convert it to a date variable, using the DATEPART() function.

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!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 2205 views
  • 3 likes
  • 4 in conversation