BookmarkSubscribeRSS Feed
Mikeyjh
Calcite | Level 5

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.

9 REPLIES 9
DBailey
Lapis Lazuli | Level 10

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

Tom
Super User Tom
Super User

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;

Astounding
PROC Star

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.

Mikeyjh
Calcite | Level 5

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.)

Scott_Mitchell
Quartz | Level 8

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.

Mikeyjh
Calcite | Level 5

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

data_null__
Jade | Level 19

INTNX

x2=intnx('DTMINUTE',x,0)
Scott_Mitchell
Quartz | Level 8

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

PGStats
Opal | Level 21

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 9 replies
  • 1365 views
  • 6 likes
  • 7 in conversation