DATA Step, Macro, Functions and more

Manipulating SAS Dates

Accepted Solution Solved
Reply
Super Contributor
Posts: 1,041
Accepted Solution

Manipulating SAS Dates

[ Edited ]

Hello,

 

I have two SAS datetimes as shown below:

I have an array of med_Time dates and want to output if med_times was 24 hours/1 day  prior to LAB_order_TIME

 

MED_TIME1                                                            LAB_ORDER_TIME

24JAN2015:20:07:00.000                                   23JAN2015:05:54:00.000

 

 

if ( LAB_ORDER_TIME -  MED_TIME) /(60*60) ge 24

 

I tried this above and not sure if i am doing it right....Would this give me the result i am looking for??

 

Thanks


Accepted Solutions
Solution
‎02-01-2016 05:46 PM
Super User
Posts: 19,770

Re: MANIPULATING SAS DATES

Posted in reply to robertrao

It should, but you should always check. Also, make sure your condition is exactly what you want, ie GE vs GT.

 

View solution in original post


All Replies
Solution
‎02-01-2016 05:46 PM
Super User
Posts: 19,770

Re: MANIPULATING SAS DATES

Posted in reply to robertrao

It should, but you should always check. Also, make sure your condition is exactly what you want, ie GE vs GT.

 

Trusted Advisor
Posts: 1,117

Re: MANIPULATING SAS DATES

Posted in reply to robertrao

Hello @robertrao,

 

Yes, this should work, provided that your criterion is "at least 24 hours prior to ..."

Super Contributor
Posts: 1,041

Re: MANIPULATING SAS DATES

Posted in reply to FreelanceReinhard

Hello ,

Sorry to add this question after having been answered.

I try to do the below and it would not somehow work for me.

Wanted to create a new varibles new_lax{} and new_time{ } for those satisfying my condition.

It would not somehow work. Could you please point out the mistake in the below code.

Thanks a  lot

 

 

 

 

 

data final;

            set orders_lax;

            by pat_enc_csn_id;                                         /*multiple records per ID exist*/

            array names(*) laxname1-laxname380;           /*med names*/

            array times(*) $ laxtime1-laxtime380;              /*med taken time*/

            array new_lax(*) $ new_lax1-new_lax100;      /*new variables which satisfied condition*/

            j=1;

            do i= 1 to 380;

            if order_time-times{i} ge 24 then do;                 /*order_time is a variable existing in dataset */

new_Lax{j}=names{i};

new_time{j}=times{i};

j+1;

end;

drop i j;

 

end;

 

run;

Super User
Posts: 19,770

Re: MANIPULATING SAS DATES

Posted in reply to robertrao

The formula you showed in your question isn't the one you coded in your code above.

Super Contributor
Posts: 1,041

Re: MANIPULATING SAS DATES

Oops. Sorry about that. Thias code is working fine but its bringing in the ones satisfying the condition as well mas others.

Could you help me grab only those which are satisfying the condition?

 

Thanks

 

 

 

data final

            set orders_lax;

            array names(*) laxname1-laxname380;                              /*med names*/

            array times(*) $ laxtime1-laxtime380;                                /*med taken time*/

            array new_lax(*) $ new_lax1-new_lax380;                        /*new variables which satisfied condition*/

            array new_time(*) new_time1-new_time380;

            j=1;

            do i= 1 to 380;

            if order_time-times{i}/60*60 ge 24 then do;                           /*order_time is a variable existing in dataset */

new_Lax{j}=names{i};                                                                    /*condition satisfied*/

new_time{j}=times{i};                                                                    /*condition satisfied*/

end;

j+1;

 

drop i j;

format new_time1-new_time350 datetime.;

end;

 

run;

Trusted Advisor
Posts: 1,117

Re: MANIPULATING SAS DATES

Posted in reply to robertrao

Your latest correction is still insufficient: You forgot two pairs of parentheses (cf. your first post in this thread):

if (order_time-times{i})/(60*60) ge 24 then do;
Super Contributor
Posts: 1,041

Re: Manipulating SAS Dates

Posted in reply to robertrao
Hi Freelance, tried adding ,removing those braces but the result would not change...
Thanks
Trusted Advisor
Posts: 1,117

Re: Manipulating SAS Dates

Posted in reply to robertrao

The inequality with the parentheses means: times{i} was at least 24 hours prior to order_time.

The inequality without parentheses means: times{i} was at least 24 seconds prior to order_time.

 

Please note that the former inequality implies the latter. That is, if all medication times were at least 24 hours prior to order time, the results would not change indeed, whether you use the correct or the incorrect formula.

 

But you wrote that your code would be "bringing in the ones satisfying the condition as well mas others." Could you please show us one of those "other" cases?

 

Also, if you are more comfortable with SAS functions than with mathematical formulas, you could rewrite your IF condition as follows:

if intck('hour', times{i}, order_time, 'c') ge 24 then do;
Super User
Posts: 19,770

Re: Manipulating SAS Dates

Posted in reply to robertrao
Well you don't exclude them anywhere. Try adding an explicit OUTPUT statement or an explicit DELETE.
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 9 replies
  • 349 views
  • 3 likes
  • 3 in conversation