Programming the statistical procedures from SAS

subsetting dates

Reply
Super Contributor
Posts: 1,040

subsetting dates

Hi,

I have to compare surgery date and lab date

surgery date stays same whereas lab_date changes and the dates are datetimes

we have to check if lab date is +/- 2 weeks from surgery date and keep only those and discard others


Surgery date         lab_date

28jun2011:9:30     09jul2011:10:00----------keep

28jun2011:9:30     16jul2011:12:00---------discard(lab date more than 2 weeks from surg date)

28jun2011:9:30     08jul2011:09:00-------keep

Thanks

Trusted Advisor
Posts: 1,137

Re: subsetting dates

please try,

data have;

    input date1 : datetime18. date2 : datetime18.;

    format date1 date2 datetime18.;

    if (datepart(date2)-datepart(date1))/7 >2 then delete;

cards;

28jun2011:9:30     09jul2011:10:00

28jun2011:9:30     16jul2011:12:00

28jun2011:9:30     08jul2011:09:00

;

run;

Thanks,

jagadish

Thanks,
Jag
Occasional Contributor
Posts: 9

Re: subsetting dates

Hi Jagadish,

                    But here you're not consider time part.I think you should time part also.

Regards,

Ramesh

Respected Advisor
Posts: 4,005

Re: subsetting dates

I probably would use intck() instead of an expression like "(datepart(date2)-datepart(date1))/7 >2"

SAS(R) 9.2 Language Reference: Dictionary, Fourth Edition

Trusted Advisor
Posts: 1,137

Re: subsetting dates

when i try to use intck function, the result in week is a integer value. so we are unable to check if the week is 2 or more than 2. as per the data provided the second record is more than 2 weeks, however with intck it is 2.

data have;

    input date1 : datetime18. date2 : datetime18.;

    format date1 date2 datetime18.;

    value1=(datepart(date2)-datepart(date1))/7;

    value=intck('week', datepart(date1), datepart(date2));

cards;

28jun2011:9:30     09jul2011:10:00

28jun2011:9:30     16jul2011:12:00

28jun2011:9:30     08jul2011:09:00

;

run;

Please check the difference between value1 and value variables. is there any way to avoid it with intck. please suggest.

Thanks,
Jag
Respected Advisor
Posts: 4,005

Re: subsetting dates

With datetime values I would use the datetime intervals with intck().

if intck('dtday', date1, date2) > 13 then delete;

Trusted Advisor
Posts: 1,137

Re: subsetting dates

Thanks Patrick , indeed a good way

Thanks,
Jag
Ask a Question
Discussion stats
  • 6 replies
  • 327 views
  • 1 like
  • 4 in conversation