- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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!
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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 |
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
The following books are very useful and helped me along the way in my SAS journey so far-
- Learning SAS® by Example: A Programmer's Guide, Second Edition- By Ron Cody
- Practical and Efficient SAS Programming: The Insider's Guide Book by Martha Messine
- Data Management Solutions Using SAS® Hash Table Operations: A Business Intelligence Case Study-By Paul Dorfman and Don Henderson
That's immense!!!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thank you!