DATA Step, Macro, Functions and more

datepart subtracting time

Reply
Contributor
Posts: 37

datepart subtracting time

Below is the code I am working with:

I am trying to get hdate to be 5 days less than tdate.  or hdate to be 5 days before tdate.

tdate =

PUT(DATEPART(Process_Date), date9.);

Format BDate hDate yymmddp10.;

BDate = intnx('day',date(),-5);

hdate = intnx('day',tdate,-5);

however I get hdate and null.

any advise

Frequent Contributor
Frequent Contributor
Posts: 94

Re: datepart subtracting time

Hi Mick,

I don't think you were too far from out.  It looks like you've mixed up what the PUT function is doing here - it converts a number to text, based on the specified format, where as you want TDATE to be an actual date value, formatted as Date9.

Try the below (the first bit just makes up some random data):

data testing;

format Process_Date datetime20.;

drop date h m s;

do i = 1 to 100;

    date = intnx('day','01JAN2011'd,365*ranuni(1)+180);

    h = 23*ranuni(2);

    m = 59*ranuni(3);

    s = 59*ranuni(4);

    Process_Date = dhms(date,h,m,s);

    output;

end;

run;

data testing2;

set testing;

format tdate date9.;

format hdate date9.;

format bdate date9.;

tdate = datepart(Process_Date);

bdate = intnx('day',date(),-5);

hdate = intnx('day',tdate,-5);

run;

Regular Contributor
Posts: 241

Re: datepart subtracting time

If you have a date value, then you can just subtract 5.

   data _null_;
     p = '24jun2011:08:30:00'dt; /* a datetime value */
     d = datepart(p); /* extract a date */
     h = d - 5; /* five days before the date */

     put p= :datetime9. (d h) (= :date9.);
   run;
   /* on log
   p=24JUN2011 d=24JUN2011 h=19JUN2011
   */

Contributor
Posts: 37

Re: datepart subtracting time

Thank you,

It works!!

It is always something so simple with SAS that catches you.

Ask a Question
Discussion stats
  • 3 replies
  • 294 views
  • 6 likes
  • 3 in conversation