Reputation: 459
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"
}
Upvotes: 0
Views: 216
Reputation: 3315
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;
Upvotes: 1