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
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
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
Or just assigne the format in procedure wanting the group. Most of the procs will honor the grouping applied by the format.
Try datetime12.
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!
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
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.