Or possibly leave the datetime value alone and use a format like DTMONYY7. when you need to use the date time grouped at month level.
Here's a sample that demonstrates the issue:
data sample;
date = dhms(today(), 11, 15, 0); /*Datetime variable*/
dtdate =datepart(date); /*Date variable*/
put date = datetime21.;
put date = dtdate9.;
put date = dtmonyy7.;
put dtdate = date9.;
put dtdate = monyy7.;
run;
And the output:
date=20JAN2016:11:15:00
date=20JAN2016
date=JAN2016
dtdate=20JAN2016
dtdate=JAN2016
NOTE: The data set WORK.SAMPLE has 1 observations and 2 variables.
NOTE: DATA statement used (Total process time):
real time 0.03 seconds
cpu time 0.01 seconds
You don't specify if you want the output to be a date or character. Here is a solution for both:
data have;
infile cards dsd;
informat date1 date9.;
format date1 date9.;
input date1;
cards;
31Dec2015
;run;
data want;
set have;
date2 = date1;
format date2 monyy7.;
date3=input(put(date2,monyy7.),$7.);
run;
Thank you all for helping out. It turns out proc sql has to interpret a date variable by the specific day, no matter how hard you try to hide it by using format. The solution is either to use proc means to get aggregates, or turn date variable into character variables. The former proved to be more natural, as it retains the chronological order of months.
Thank you so much.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.