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.
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
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;
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.
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.)
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.
Thanks Scott. So easy now I see what you've done.
INTNX
That is fantastic data_null. I didn't realisze that you could use the DT prefix with time intervals.
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
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.