BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Kimberley
Calcite | Level 5

I am struggling at finding a way to group observations by date and hour from a variable that is in a datetime21.2 format.

Any ideas? I am hoping this is a very easy question for someone, as I can't seem to find a way to do it (Using SAS 8.2)

Thank you for your help!

Kim

1 ACCEPTED SOLUTION

Accepted Solutions
JasonDiVirgilio
Quartz | Level 8

Something like this?:

data _null_;

  mydate=mdy(1,5,2012);

  mydatetime=dhms(mydate,10,42,59);

  format mydatetime datetime21.2;

  put mydatetime=;

  mynewvar=put(day(mydate),z2.)||upcase(put(month(mydate),monname3.))||trim(left(year(mydate)))||':'||put(hour(mydatetime),z2.);

  put mynewvar=;

run;

...gives....

mydatetime=05JAN2012:10:42:59.00

mynewvar=05JAN2012:10

View solution in original post

4 REPLIES 4
JasonDiVirgilio
Quartz | Level 8

Something like this?:

data _null_;

  mydate=mdy(1,5,2012);

  mydatetime=dhms(mydate,10,42,59);

  format mydatetime datetime21.2;

  put mydatetime=;

  mynewvar=put(day(mydate),z2.)||upcase(put(month(mydate),monname3.))||trim(left(year(mydate)))||':'||put(hour(mydatetime),z2.);

  put mynewvar=;

run;

...gives....

mydatetime=05JAN2012:10:42:59.00

mynewvar=05JAN2012:10

ballardw
Super User

Or just assigne the format in procedure wanting the group. Most of the procs will honor the grouping applied by the format.

Try datetime12.

Kimberley
Calcite | Level 5

Finished *Pulling Out My Hair*, thank you!

Ballardw, I tried doing this first as I though it would be the cleanest way, but it did not seem to work in  proc shewart. Thank you for your help!

Jason, Thanks for the code, I used teh essence of it, and made it work. Thank you!

Ksharp
Super User

I like ballardw 's idea which is my first option.

Try this:

proc format;
picture fmt 
 low-high='%0d%b%Y:%H'(datatype=datetime) ;
run;


data _null_;
  mydate=mdy(1,5,2012);
  mydatetime=dhms(mydate,10,42,59);
  format mydatetime fmt20.;
  put mydatetime=;
run;

Ksharp

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
  • 4 replies
  • 1744 views
  • 6 likes
  • 4 in conversation