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

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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