BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ballardw
Super User

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.

Reeza
Super User

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

 

Steelers_In_DC
Barite | Level 11

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;

apolitical
Obsidian | Level 7

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.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 18 replies
  • 147874 views
  • 5 likes
  • 7 in conversation