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!
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;
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!
Yes, sorry I haven't done that. Here is the print out of the dataset i need. Discont is the new variable.
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 |
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;
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!
The following books are very useful and helped me along the way in my SAS journey so far-
That's immense!!!
Thank you!
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.