turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- SAS Procedures
- /
- SAS datatime variables

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

08-05-2013 10:58 AM

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

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:

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

08-05-2013 11:56 AM

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;

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

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?

Good luck.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

08-08-2013 05:08 AM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

08-08-2013 05:25 AM

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

08-08-2013 06:11 AM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

08-08-2013 07:35 AM

INTNX

x2=intnx('DTMINUTE',x,**0**)

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

08-08-2013 07:33 PM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

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.

PG

PG