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

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
novinosrin
Tourmaline | Level 20

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;




 

View solution in original post

6 REPLIES 6
novinosrin
Tourmaline | Level 20

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!

Dinurik
Fluorite | Level 6

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
1100
1200
1310
1410
2121
2221
2321
2401
3111
3211
3301
3411
4110
4210
4310
4410
novinosrin
Tourmaline | Level 20

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;




 

Dinurik
Fluorite | Level 6

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!

novinosrin
Tourmaline | Level 20

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-By Paul Dorfman and Don Henderson

That's immense!!!

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 6 replies
  • 593 views
  • 0 likes
  • 2 in conversation