Help using Base SAS procedures

SAS datatime variables

Reply
Contributor
Posts: 28

SAS datatime variables

Hi,

I have 2 SAS datatime fields - variable X collects a seconds component and variable Y does not (Y reports seconds as :00). How can I convert variable X so that it has a 00 seconds component so that I can compare the values as X = Y (so matches within DMYHM components only)?

Thanks for your help.

Super Contributor
Posts: 578

Re: SAS datatime variables

If you have two datetime columns, then they both include seconds.  Don't confuse the format with the underlying value.  You can convert a date variable into a datetime by using the dhms function:

dhms('05Aug2013'd,0,0,0)='05Aug2013:0:0'dt

Super User
Super User
Posts: 6,498

Re: SAS datatime variables

You can remove the time part of a datetime value a number of way.

data _null_;

  X='01JAN2013:12:30'dt;

  Y='01JAN2013:00:00'dt ;

* Use DATEPART() function on both variables. ;

  if datepart(X) = datepart(Y) then put '1 equal';

* Use INTNX() function to "round" the value ;

  if  y = intnx('dtday',x,0) then put '2 equal';

* Use DATEPART() and DHMS() functions on one. ;

  if y = dhms(datepart(X),0,0,0) then put '3 equal';

run;

Super User
Posts: 5,077

Re: SAS datatime variables

If variety is the spice of life:

if y = x - mod(x,60) then put '4 equal';

It also makes a difference as to what Y actually contains.  If the actual time would have been at the 59 second mark, would the seconds be ignored or would the minutes be rounded upward?

Good luck.

Contributor
Posts: 28

Re: SAS datatime variables

Thanks for your replies but I'm still not getting it. So if I have 2 datetime variables

X=22MAY13:10:44:00

Y=22MAY12:10:44:15

I need to effectively default the Y seconds to 00 so that I apply a X=Y condition that is applied down to the minute component.

(I expect Im being dumb.)

Super Contributor
Posts: 297

Re: SAS datatime variables

Hi Mike,

The following will create a variable X1 which contains an augmented version of the X variable with the seconds converted to 0 - which is what the 4th argument in the DHMS statement represents.  You can then compare the Y and X1 variables.

DATA WANT;

MATCH = 0;

X="22MAY13:10:44:15"DT;

Y="22MAY13:10:44:00"DT;

X1=DHMS(DATEPART(X),HOUR(X),MINUTE(X),0);

IF Y = X1 THEN MATCH = 1;

FORMAT X X1 Y DATETIME18.;

RUN;

Of course you could also do the following, which negates the need for the additional variable.:

DATA WANT;

MATCH = 0;

X="22MAY13:10:44:15"DT;

Y="22MAY13:10:44:00"DT;

IF Y = DHMS(DATEPART(X),HOUR(X),MINUTE(X),0) THEN MATCH = 1;

FORMAT X Y DATETIME18.;

RUN;

Regards,

Scott

Edit: Removed Timepart references as they are not necessary.

Contributor
Posts: 28

Re: SAS datatime variables

Thanks Scott. So easy now I see what you've done.

Respected Advisor
Posts: 3,777

Re: SAS datatime variables

INTNX

x2=intnx('DTMINUTE',x,0)
Super Contributor
Posts: 297

Re: SAS datatime variables

That is fantastic data_null.  I didn't realisze that you could use the DT prefix with time intervals.

Respected Advisor
Posts: 4,641

Re: SAS datatime variables

Note: You CAN use a DT prefix with datetime or time intervals but you don't have to for SECOND, MINUTE and HOUR. You do have to use the DT prefix for DAY and longer intervals (i.e. DTMONTH) to tell them apart from date intervals. Of course, you do have to use the corresponding date or datetime values for the other arguments. SAS (as of 9.3 at least) doesn't check for proper correspondence.

PG

PG
Ask a Question
Discussion stats
  • 9 replies
  • 349 views
  • 6 likes
  • 7 in conversation