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

## Create an indicator conditional on change of values within subjects

Hi SAS programmers,

Could you please help me with this problem? I don't even know where to start learning how to do this and not sure whether it is possible.

This is the data:

data a;
input id time therapy;
cards;
1 1 0
1 2 0
1 3 1
1 4 1
2 1 2
2 2 2
2 3 2
2 4 0
3 1 1
3 2 1
3 3 0
3 4 1
4 1 1
4  2 1
4 3 1
4 4 1
;
run;

I need to create a time-independent indicator variable that takes the value of 1 if subject's therapy became 0 after being 1, or 2; and 0 otherwise. So, suppose therapy=0 means no therapy, while therapies 1 and 2 are types of therapy. If 0 precedes 1 or 2, it is considered it to be normal, while if it follows 1 or 2, it means discontinuation of therapy, which required attention. I ultimately need to know how many people discontinued therapy. Is it at all possible to do in SAS?

Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
Tourmaline | Level 20

## Re: Create an indicator conditional on change of values within subjects

Hi @Dinurik  Thank you. Please try -

``````

data a;
input id time therapy;
cards;
1 1 0
1 2 0
1 3 1
1 4 1
2 1 2
2 2 2
2 3 2
2 4 0
3 1 1
3 2 1
3 3 0
3 4 1
4 1 1
4  2 1
4 3 1
4 4 1
;
run;

data discontinued_therapy;
do _n_=1 by 1 until(last.id);
set a;
by id;
if not therapy and not first.id and lag(therapy) then discont=1;
end;
do _n_=1 to _n_;
set a;
discont=^^discont;
output;
end;
run;

``````

6 REPLIES 6
Tourmaline | Level 20

## Re: Create an indicator conditional on change of values within subjects

Hi @Dinurik  Can you please post your expected output for the input sample you posted. This can help avoid readers in assuming things that may be inaccurate. Thank you!

Fluorite | Level 6

## Re: Create an indicator conditional on change of values within subjects

Yes, sorry I haven't done that. Here is the print out of the dataset i need. Discont is the new variable.

id    time  therapy discont
 1 1 0 0 1 2 0 0 1 3 1 0 1 4 1 0 2 1 2 1 2 2 2 1 2 3 2 1 2 4 0 1 3 1 1 1 3 2 1 1 3 3 0 1 3 4 1 1 4 1 1 0 4 2 1 0 4 3 1 0 4 4 1 0
Tourmaline | Level 20

## Re: Create an indicator conditional on change of values within subjects

Hi @Dinurik  Thank you. Please try -

``````

data a;
input id time therapy;
cards;
1 1 0
1 2 0
1 3 1
1 4 1
2 1 2
2 2 2
2 3 2
2 4 0
3 1 1
3 2 1
3 3 0
3 4 1
4 1 1
4  2 1
4 3 1
4 4 1
;
run;

data discontinued_therapy;
do _n_=1 by 1 until(last.id);
set a;
by id;
if not therapy and not first.id and lag(therapy) then discont=1;
end;
do _n_=1 to _n_;
set a;
discont=^^discont;
output;
end;
run;

``````

Fluorite | Level 6

## Re: Create an indicator conditional on change of values within subjects

Hi @novinosrin,

Thank you so much! I wrote my one extremely inefficient code involving  proc expand to compute the lagged therapy, and creating too many intermediate variables. It was so long and ugly that I didn't dare to share it. Thank you for this short and elegant beauty! Could you please refer me to any education materials where I could educate myself on this kind of programming in SAS?

Thanks again!

Tourmaline | Level 20

## Re: Create an indicator conditional on change of values within subjects

The following books are very useful and helped me along the way in my SAS journey so far-

1. Learning SAS® by Example: A Programmer's Guide, Second Edition- By Ron Cody
2. Practical and Efficient SAS Programming: The Insider's Guide Book by Martha Messine
3. Data Management Solutions Using SAS® Hash Table Operations: A Business Intelligence Case Study-

That's immense!!!

Fluorite | Level 6

## Re: Create an indicator conditional on change of values within subjects

Thank you!

Discussion stats
• 6 replies
• 816 views
• 0 likes
• 2 in conversation