08-05-2013 10:58 AM
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.
08-05-2013 11:27 AM
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:
08-05-2013 11:56 AM
You can remove the time part of a datetime value a number of way.
* 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';
08-05-2013 12:48 PM
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?
08-08-2013 05:08 AM
Thanks for your replies but I'm still not getting it. So if I have 2 datetime variables
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.)
08-08-2013 05:25 AM
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.
MATCH = 0;
IF Y = X1 THEN MATCH = 1;
FORMAT X X1 Y DATETIME18.;
Of course you could also do the following, which negates the need for the additional variable.:
MATCH = 0;
IF Y = DHMS(DATEPART(X),HOUR(X),MINUTE(X),0) THEN MATCH = 1;
FORMAT X Y DATETIME18.;
Edit: Removed Timepart references as they are not necessary.
08-08-2013 10:18 PM
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.