Kevin Grossman
Kevin Grossman

Reputation:

Creating a dummy variable for ``switching''

I'm working on a project in SAS and I wanted to create a dummy variable that accounted for ``preferences in medicine''. I have a long data-set, by time period, of individuals taking either medicine type 1 or type 2. For my research, I want to create a variable to represent if individuals who take type 1 medicine, then switched to type 2, but went back to type 1. I am unconcerned with the time interval that the individual was on the medication for, just that they followed this pattern.

      id  month  type
      1    1       2
      1    2       2
      1    3       2
      2    1       1
      2    2       2
      2    3       1      
             ...

I have more months, but just wanted to provide something to elucidate what I'm trying to get. Basically, I want to tally those subjects who are like subject 2.

Upvotes: 1

Views: 1091

Answers (4)

JMJ1737
JMJ1737

Reputation: 1

I think the following should work:

 DATA Have; 
 input id month type; 
 if _n_ ^= 1 and id ^= lag(id) then diftype = .;
 else diftype = dif(type);
 datalines;
 1 1 1
 1 2 1
 1 3 1
 1 4 1
 1 5 1
 2 1 1
 2 2 2
 2 3 1
 2 4 1
 2 5 1
 3 1 1
 3 2 1
 3 3 2
 3 4 2
 3 5 1
 4 1 2
 4 2 2
 4 3 2
 4 4 2
 4 5 2
 ;

proc sql;
     select case when max(diftype) = 1 and min(diftype) = -1 then 1 else 0 end as flag, * from have
  group by id
  ;

quit;

Upvotes: 0

Longfish
Longfish

Reputation: 7602

I prefer @CarolinaJay65 approach, it's a lot cleaner and just involves one pass of the data. If all you are interested in are the patients who start and finish on Type1, but use Type2 at some point, then the code can be simplified slightly. The following code (using @CarolinaJay65 source data) will only output the patient_id's matching this criteria.

data switch_id (keep=id);
set have;
by id month;
retain switch;
if first.id then do;
    call missing(switch);
    if type=1 then switch=0;
    end;
else if not missing(switch) and type=2 then switch=1;
if last.id and type=1 and switch=1 then output;
run;

If you just wanted the number of patients who match the criteria then you could tweak this code further.

data switch (keep=count);
set have end=final;
by id month;
retain switch count 0;
if first.id then do;
    call missing(switch);
    if type=1 then switch=0;
    end;
else if not missing(switch) and type=2 then switch=1;
if last.id and type=1 and switch=1 then count+1;
if final then output;
run;  

Upvotes: 0

William Whitworth
William Whitworth

Reputation: 11

well, nothing fancy, but it works for me:

DATA LONG1;
input id  month  type;
cards;
1    1       2
1    2       2
1    3       2
1    4       2
1    5       2
1    6       2
1    7       2
1    8       2
1    9       2
1   10       2
2    1       1
2    2       1
2    3       1
2    4       1
2    5       1
2    6       1
2    7       1
2    8       1
2    9       1
2   10       1
3    1       1
3    2       1
3    3       1
3    4       2
3    5       1
3    6       1
3    7       1
3    8       1
3    9       1
3   10       1
;

Proc Print; run;
* 1) make a wide dataset by deconstructing the initial long data by month & rejoining by id
2) then use if/then statements to create your dummy variable, 
3) then merge the dummy variable back into your long dataset using ID;

DATA month1; set long1; where month=1; rename month=month_1 type=type_1; Proc Sort; by ID; run;
DATA month2; set long1; where month=2; rename month=month_2 type=type_2; Proc Sort; by ID; run;
DATA month3; set long1; where month=3; rename month=month_3 type=type_3; Proc Sort; by ID; run;
DATA month4; set long1; where month=4; rename month=month_4 type=type_4; Proc Sort; by ID; run;
DATA month5; set long1; where month=5; rename month=month_5 type=type_5; Proc Sort; by ID; run;
DATA month6; set long1; where month=6; rename month=month_6 type=type_6; Proc Sort; by ID; run;
DATA month7; set long1; where month=7; rename month=month_7 type=type_7; Proc Sort; by ID; run;
DATA month8; set long1; where month=8; rename month=month_8 type=type_8; Proc Sort; by ID; run;
DATA month9; set long1; where month=9; rename month=month_9 type=type_9; Proc Sort; by ID; run;
DATA month10; set long1; where month=10; rename month=month_10 type=type_10; Proc Sort; by ID; run;


DATA WIDE;
merge month1 month2 month3 month4 month5 month6 month7 month8 month9 month10; by ID; 
if (type_1=1 and type_2=1 and type_3=1 and type_4=1 and type_5=1 
and type_6=1 and type_7=1 and type_8=1 and type_9=1 and type_10=1) or 
(type_1=2 and type_2=2 and type_3=2 and type_4=2 and type_5=2 
and type_6=2 and type_7=2 and type_8=2 and type_9=2 and type_10=2) 
then switch='no '; else switch='yes '; keep ID switch; run;

DATA LONG2;
merge wide long1; by ID;
Proc Print; run;

btw: also go to the SAS listserv, they love stuff like this: http://www.listserv.uga.edu/archives/sas-l.html

Upvotes: 1

Jay Corbett
Jay Corbett

Reputation: 28441

This worked on the limited data I used:

DATA Have; 
 input id month type; 
 datalines;
 1 1 1
 1 2 1
 1 3 1
 1 4 1
 1 5 1
 2 1 1
 2 2 2
 2 3 1
 2 4 1
 2 5 1
 3 1 1
 3 2 1
 3 3 2
 3 4 2
 3 5 1
 4 1 2
 4 2 2
 4 3 2
 4 4 2
 4 5 2
 ;

Data Temp(keep=id dummy);
 length dummy $15;
 retain Start Type2 dummy;
 set Have;
 by id;

 if first.id then Do;
  Start=0;
  Type2=0;
  Dummy="";
 end;

 If Type=1 then do;
  If Start=0 then Start=1;
  else if Start=1 and Type2=1 then Dummy="Switch-er-Roo";
 end;
 else do;
  if Start=1 then Type2=1;
 end;

 if last.id then output;
run;

Data Want;
 merge temp(in=a) have(in=b);
 by id;
run;

Upvotes: 0

Related Questions