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.
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.
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.
thanks for that, it worked
data _null_;
dt="23MAY2013:00:00:00"dt;
year=year(datepart(dt));
put year=;
run;
great, thanks for your help. it worked
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;
@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.
Yes, you are right. data test;
x1 = '01jan2018xxxxxxxxxxx'd;
format x1 yymmddd10.;
run;
Output dataset contain
Obs x1
1 2018-01-01
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 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.