Good day SAS Community,
I have a question concerning manipulation of dates in SAS. Suppose I have a numerical variable for year (ex: 2016, 2017, etc...) and I instead wanted to generate a second date (DATE9.) variable that would be set to January 1st, for each of those years (ex: 2016-01-01, 2017-01-01).
How would I go about doing this in SAS?
id | year | new_date |
1 | 2015 | 01JAN2015 |
2 | 2014 | 01JAN2014 |
3 | 2008 | 01JAN2008 |
4 | 1999 | 01JAN1999 |
Thank you again for your help,
-Carmine
if year is numeric years
new_date=mdy(1,1,year);
format new_date date9.;
data w;
do year=2015 to 2025;
new_date=mdy(1,1,year);
output;
end;
format new_date date9.;
run;
@Carmine_Rossi,
You may try this :
data want;
set have;
dt = DatePart(new_date);
Year1 = Year(dt);
Month1 = Month(dt);
Day1 = Day(dt);
Hour1 = Hour(dt);
run;
if year is numeric years
new_date=mdy(1,1,year);
format new_date date9.;
data w;
do year=2015 to 2025;
new_date=mdy(1,1,year);
output;
end;
format new_date date9.;
run;
Thank you very much! The MDY function is what I was looking for!
-Carmine
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.