DATA Step, Macro, Functions and more

To get only Year from DATETIME20. format

Reply
Contributor
Posts: 27

To get only Year from DATETIME20. format

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.

 

Super User
Posts: 10,244

Re: To get only Year from DATETIME20. format

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;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Contributor
Posts: 27

Re: To get only Year from DATETIME20. format

Posted in reply to KurtBremser

thanks for that, it worked

PROC Star
Posts: 1,283

Re: To get only Year from DATETIME20. format

data _null_;
   dt="23MAY2013:00:00:00"dt;
   year=year(datepart(dt));
   put year=;
run;
Contributor
Posts: 27

Re: To get only Year from DATETIME20. format

great, thanks for your help. it worked

Contributor
Posts: 20

Re: To get only Year from DATETIME20. format

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;

Contributor
Posts: 27

Re: To get only Year from DATETIME20. format

Posted in reply to katkarparam
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
Super User
Posts: 10,244

Re: To get only Year from DATETIME20. format

@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.

 

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Contributor
Posts: 27

Re: To get only Year from DATETIME20. format

Posted in reply to KurtBremser
Will check on Monday thanks for ur help...
Contributor
Posts: 20

Re: To get only Year from DATETIME20. format

[ Edited ]
Posted in reply to KurtBremser

Yes, you are right. data test;

x1 = '01jan2018xxxxxxxxxxx'd;

format x1 yymmddd10.;

run;

 

 

Output dataset contain

 

 

Obs          x1

1             2018-01-01

Contributor
Posts: 20

Re: To get only Year from DATETIME20. format

I will let you know..
Ask a Question
Discussion stats
  • 10 replies
  • 124 views
  • 0 likes
  • 4 in conversation