DATA Step, Macro, Functions and more

Need Date Format like YYYYMMDDHH for the previous hour

Accepted Solution Solved
Reply
SAS Employee o_p
SAS Employee
Posts: 8
Accepted Solution

Need Date Format like YYYYMMDDHH for the previous hour

Hi All,

I want to create a variable which shows the previous hour.

The format of this variable will be YYYYMMDDHH. --> ($CHAR10.)

Assume that; now it is 2014102322, and I want to create my variable with 2014102321 which means it is one hour before the current time.

If it is 2014102400 then the value should be 2014102323.

How can I code that?

Thanks,

Onuralp


Accepted Solutions
Solution
‎10-23-2014 04:59 PM
SAS Super FREQ
Posts: 709

Re: Need Date Format like YYYYMMDDHH for the previous hour

you can create your own datetime format to get the what you want. The INTNX function can be used to add or subtract datetime intervals, see sample

below:

proc format;

  picture xyyyymmddhh (default=10)

    low - high = '%Y%m%0d%0H' (datatype=datetime)

  ;

run;

data test;

  now = datetime();

  length now_c10 $ 10;

  now_c10 = put(now, xyyyymmddhh.);

  now1h = intnx("dthour", now, -1);

  now_c101h = put(now1h, xyyyymmddhh.);

 

  format now datetime22.3;

run;

View solution in original post


All Replies
Solution
‎10-23-2014 04:59 PM
SAS Super FREQ
Posts: 709

Re: Need Date Format like YYYYMMDDHH for the previous hour

you can create your own datetime format to get the what you want. The INTNX function can be used to add or subtract datetime intervals, see sample

below:

proc format;

  picture xyyyymmddhh (default=10)

    low - high = '%Y%m%0d%0H' (datatype=datetime)

  ;

run;

data test;

  now = datetime();

  length now_c10 $ 10;

  now_c10 = put(now, xyyyymmddhh.);

  now1h = intnx("dthour", now, -1);

  now_c101h = put(now1h, xyyyymmddhh.);

 

  format now datetime22.3;

run;

SAS Employee o_p
SAS Employee
Posts: 8

Re: Need Date Format like YYYYMMDDHH for the previous hour

Posted in reply to Bruno_SAS

Thanks Bruno.

This is definitely what I was looking for. Smiley Happy

Regards,

Olp

🔒 This topic is solved and locked.

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

Discussion stats
  • 2 replies
  • 208 views
  • 1 like
  • 2 in conversation