DATA Step, Macro, Functions and more

How can I create a variable with a ddmmmyyy:hh format from a variable with a datetime21.2 format?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 12
Accepted Solution

How can I create a variable with a ddmmmyyy:hh format from a variable with a datetime21.2 format?

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


Accepted Solutions
Solution
‎05-22-2012 03:01 PM
Contributor
Posts: 44

Re: How can I create a variable with a ddmmmyyy:hh format from a variable with a datetime21.2 format?

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


All Replies
Solution
‎05-22-2012 03:01 PM
Contributor
Posts: 44

Re: How can I create a variable with a ddmmmyyy:hh format from a variable with a datetime21.2 format?

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

Super User
Posts: 10,454

Re: How can I create a variable with a ddmmmyyy:hh format from a variable with a datetime21.2 format?

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

Try datetime12.

Occasional Contributor
Posts: 12

Re: How can I create a variable with a ddmmmyyy:hh format from a variable with a datetime21.2 format?

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!

Super User
Posts: 9,656

Re: How can I create a variable with a ddmmmyyy:hh format from a variable with a datetime21.2 format?

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

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

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