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.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

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