0

I am attempting to create a variable that will store a list of strings related to other variables in a SAS data set. Here is a sample of the data set:

Patient  Therapy1  Therapy2 Therapy3 Therapy4
1001       X           X       X        X
1002       X                   X            
1003                   X                X

Basically I want to append a variable at the end of the list called "Summary" that will contain a string of the therapies for each patient. So Patient 1001 should say "Therapy1, Therapy2, Therapy3, Therapy4", Patient 1002 should say "Therapy1, Therapy3", and Patient 1003 should say "Therapy2, Therapy4".

The code for this data is stored in a table called tmp.

Edit: I'm going to write this out in R and then see if someone can do something similar in SAS.

for(i in 1:nrows(data)){
  if(data$Therapy1[i] == "X"){
    data$Summary[i] = data$Summary[i] + ", " + "Therapy1"
    }
    ....
   if(data$Therapy4[i] == "X"){
    data$Summary[i] = data$Summary[i] + " " + "Therapy4"
    }
4
  • What is your question? Do you have any existing code and directed questions about that code that we can help you with? Commented Aug 31, 2018 at 20:38
  • I added some similar code in R. Maybe that will be useful. Commented Aug 31, 2018 at 20:46
  • This almost exact question was asked on SAS Communities a few minutes ago. communities.sas.com/t5/SAS-Programming/… There's a working solution there as well. Commented Aug 31, 2018 at 20:58
  • @Reeza Thank you! Commented Aug 31, 2018 at 21:01

1 Answer 1

1

one way to do this is using array and vname as shown below

data want;
set have;
length summary $100.;
array new(*) therapy:;
do i = 1 to dim(new);
if new(i) ne ' '
then summary=catx(',',summary,vname(new(i)));
end;
drop i;
run;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.