- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
thanks for that, it worked
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
data _null_;
dt="23MAY2013:00:00:00"dt;
year=year(datepart(dt));
put year=;
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
great, thanks for your help. it worked
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Yes, you are right. data test;
x1 = '01jan2018xxxxxxxxxxx'd;
format x1 yymmddd10.;
run;
Output dataset contain
Obs x1
1 2018-01-01
- Tags:
- kurtBremser
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.