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

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 998 views
  • 6 likes
  • 4 in conversation