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

Hi Friends,

I need your help in year format.

I have a data set which has got commencement_dt as 23MAY2013:00:00:00 which is in DATETIME20. format, from the same commencement_dt i want to get only year. Is there a way where i can get only year format or by creating another variable only for year to appear.

 

Appreciate your help.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Use the datepart() and year() functions:

data have;
input commencement_dt :datetime20.;
format commencement_dt datetime20.;
cards;
23MAY2013:00:00:00
;
run;

data want;
set have;
commencement_year = year(datepart(commencement_dt));
run;

 

You can also use the DTYEAR format to display just the year from a datetime value in SAS -- no need to create a different variable.

View solution in original post

11 REPLIES 11
Kurt_Bremser
Super User

Use the datepart() and year() functions:

data have;
input commencement_dt :datetime20.;
format commencement_dt datetime20.;
cards;
23MAY2013:00:00:00
;
run;

data want;
set have;
commencement_year = year(datepart(commencement_dt));
run;

 

You can also use the DTYEAR format to display just the year from a datetime value in SAS -- no need to create a different variable.

BIDD
Fluorite | Level 6

thanks for that, it worked

PeterClemmensen
Tourmaline | Level 20
data _null_;
   dt="23MAY2013:00:00:00"dt;
   year=year(datepart(dt));
   put year=;
run;
BIDD
Fluorite | Level 6

great, thanks for your help. it worked

katkarparam
Fluorite | Level 6

we can use Year() fun to extract the year from sas date value.

 

Data    Demo;

Var1='21feb2002:22:23:00'd;

var2=year( var1);

run;

BIDD
Fluorite | Level 6
Hi, thanks for that... My requirement is;i have over 1000 records on
commencement_date variable, if you can provide me the solution to change at
one shot for all rather than one date.. It would be great ...Sorry I forgot
to mention that in the post
Kurt_Bremser
Super User

@katkarparam only wanted to show that SAS accepts any longer string as a date literal, as long as the start is a SAS date:

data test;
x1 = '01jan2018xxxxxxxxxxx'd;
format x1 yymmddd10.;
run;

For existing data, use a data step similar to mine.

 

BIDD
Fluorite | Level 6
Will check on Monday thanks for ur help...
katkarparam
Fluorite | Level 6

Yes, you are right. data test;

x1 = '01jan2018xxxxxxxxxxx'd;

format x1 yymmddd10.;

run;

 

 

Output dataset contain

 

 

Obs          x1

1             2018-01-01

katkarparam
Fluorite | Level 6
I will let you know..
hnb_matt_d
Obsidian | Level 7

Realize this is a while following the original post, but I got this to work in a select proc sql statement with an existing datetime20. formatted variable in a table:

 

year(datepart(table_cases.item_date)) as ITEM_YEAR

 

It's a numerical variable, but it worked.  

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 11 replies
  • 38481 views
  • 3 likes
  • 5 in conversation