BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Willy
Calcite | Level 5

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!!!!!!

1 ACCEPTED SOLUTION

Accepted Solutions
Haikuo
Onyx | Level 15

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_:);

_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

4 REPLIES 4
Haikuo
Onyx | Level 15

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

Haikuo

Willy
Calcite | Level 5

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.

Haikuo
Onyx | Level 15

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_:);

_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

Willy
Calcite | Level 5

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

This is what i want!   Thanks!

Smiley Happy

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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