How to capture the overlap time?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 7
Accepted Solution

How to capture the overlap time?

Hello every SAS programmers,

I have one troublesome question which need your talented expertise to solve it.

We can see the first picture. There are several variables.(id, metformin, SU, acar,meg and its start day and censor day)

擷取.JPG

And I want to find the overlapped period for metformin and other variables.

I also restrict the period must contain metformin and other one variable.

That is to say, i restrict the period must only contain metformin+SU/acar/meg.

Then i will name the start of the period as index day and the end of the period will be named as censor day.

The samples below can help you understand my definition.

圖片1.png

And after find the index day and censor day, i still have one small problem.

Take ex.3 as sample.

Because the person has three medications(metfomrin,SU,TZD),therefore the variables will be "1".

However, because of the period(index day to censor day) only contain Metformin and TZD, thus I hope that SU can change to "0" and Metformin,TZD still be "1".

The following picture is the result i want.

圖片2.png

I have to apologize for the lengthy problem...

The problem have bothered me a long time but i still can't figure out the solution.

Hope you can understand and solve my question. Very very very very thank you!!!!!!


Accepted Solutions
Solution
‎02-03-2014 12:25 PM
Respected Advisor
Posts: 3,156

Re: How to capture the overlap time?

The following code only captures the very first period that meets your conditions. You can tweak it a little if you want to capture them all. Also you can reorganize your variables names and add some Array(), code could be less hard coding.

libname _01 "H:\Temp\all";

data want;

set _01.all_user_met;

format index_day censor_day mmddyy10.;

_start= min(of start_Smiley Happy;

_end = max (met_censor, su_censor, acar_censor, meg_censor, tzd_censor);

call missing (_flash);

do _day=_start to _end until (_flash>1);

        metformin=ifn(start_met_day <= _day <= met_censor,1,0);

        su=ifn(start_su_day <= _day <= su_censor,1,0);

        acar=ifn(start_acar_day <= _day <= acar_censor,1,0);

        meg=ifn(start_meg_day <= _day <= meg_censor,1,0);

        tzd=ifn(start_tzd_day <= _day <= tzd_censor,1,0);

        _flag=sum(metformin, su,acar,meg,tzd);

        _status=( _flag=2 and metformin=1);

       _flash0=ifn(mod(_day,2)=0,_status,_flash0);

        _flash1=ifn(mod(_day,2)=1,_status, _flash1);

        if _flash0 ne _flash1 and _status=1 then _flash+1;

        if _status=1 and _flash=1 then do;

             index_day=min(index_day,_day);

                 censor_day=max(censor_day,_day);

           end;

     end;

/*reset flags*/

     if not missing (index_day+censor_day) then do;

        metformin=ifn(start_met_day <= index_day and met_censor >= censor_day,1,0);

        su=ifn(coalesce(start_su_day,constant('big')) <= index_day and  censor_day <= su_censor,1,0);

        acar=ifn(coalesce(start_acar_day,constant('big')) <= index_day and censor_day<= acar_censor,1,0);

        meg=ifn(coalesce(start_meg_day,constant('big')) <= index_day and censor_day <= meg_censor,1,0);

        tzd=ifn(coalesce(start_tzd_day,constant('big')) <= index_day and censor_day <= tzd_censor,1,0);

      end;

      drop _:;

run;

Haikuo

View solution in original post


All Replies
Respected Advisor
Posts: 3,156

Re: How to capture the overlap time?

I wonder if you could upload a sample data for us to work on? Sometimes I find blind coding a bit frustrating.

Haikuo

Occasional Contributor
Posts: 7

Re: How to capture the overlap time?

That's ok.

I have uploaded my sas file.

Hope you can solve my problem......Thank you!

If you still have any question, you can just reply here.

I will try my best to answer you ASAP.

Attachment
Solution
‎02-03-2014 12:25 PM
Respected Advisor
Posts: 3,156

Re: How to capture the overlap time?

The following code only captures the very first period that meets your conditions. You can tweak it a little if you want to capture them all. Also you can reorganize your variables names and add some Array(), code could be less hard coding.

libname _01 "H:\Temp\all";

data want;

set _01.all_user_met;

format index_day censor_day mmddyy10.;

_start= min(of start_Smiley Happy;

_end = max (met_censor, su_censor, acar_censor, meg_censor, tzd_censor);

call missing (_flash);

do _day=_start to _end until (_flash>1);

        metformin=ifn(start_met_day <= _day <= met_censor,1,0);

        su=ifn(start_su_day <= _day <= su_censor,1,0);

        acar=ifn(start_acar_day <= _day <= acar_censor,1,0);

        meg=ifn(start_meg_day <= _day <= meg_censor,1,0);

        tzd=ifn(start_tzd_day <= _day <= tzd_censor,1,0);

        _flag=sum(metformin, su,acar,meg,tzd);

        _status=( _flag=2 and metformin=1);

       _flash0=ifn(mod(_day,2)=0,_status,_flash0);

        _flash1=ifn(mod(_day,2)=1,_status, _flash1);

        if _flash0 ne _flash1 and _status=1 then _flash+1;

        if _status=1 and _flash=1 then do;

             index_day=min(index_day,_day);

                 censor_day=max(censor_day,_day);

           end;

     end;

/*reset flags*/

     if not missing (index_day+censor_day) then do;

        metformin=ifn(start_met_day <= index_day and met_censor >= censor_day,1,0);

        su=ifn(coalesce(start_su_day,constant('big')) <= index_day and  censor_day <= su_censor,1,0);

        acar=ifn(coalesce(start_acar_day,constant('big')) <= index_day and censor_day<= acar_censor,1,0);

        meg=ifn(coalesce(start_meg_day,constant('big')) <= index_day and censor_day <= meg_censor,1,0);

        tzd=ifn(coalesce(start_tzd_day,constant('big')) <= index_day and censor_day <= tzd_censor,1,0);

      end;

      drop _:;

run;

Haikuo

Occasional Contributor
Posts: 7

Re: How to capture the overlap time?

THANK YOU  Hai.kuo !!!!!  Awesome!!!

This is what i want!   Thanks!

Smiley Happy

🔒 This topic is solved and locked.

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

Discussion stats
  • 4 replies
  • 249 views
  • 0 likes
  • 2 in conversation