Hi,
Wonder if you can help. I'm trying to create a new variable which brings in variable names where they equal 1.
For example Reason is the variable I want to create.
Application_number | A | B | C | Reason |
5002 | 1 | 1 | 0 | A,B |
5003 | 1 | 1 | 1 | A,B,C |
5004 | 0 | 0 | 1 | C |
5005 | 1 | 1 | 1 | A,B,C |
I've tried a VNAME but could only get it to bring in 1 hit. My data source has 49 variables that could have a possible 1.
Thanks
Dan
Try this
data have;
input Application_number A B C;
datalines;
5002 1 1 0
5003 1 1 1
5004 0 0 1
5005 1 1 1
;
data want;
set have;
array rr A B C;
length Reason $ 100;
do over rr;
if rr then Reason = catx(',', Reason, vname(rr));
end;
run;
Result:
Application_number A B C Reason 5002 1 1 0 A,B 5003 1 1 1 A,B,C 5004 0 0 1 C 5005 1 1 1 A,B,C
Try this
data have;
input Application_number A B C;
datalines;
5002 1 1 0
5003 1 1 1
5004 0 0 1
5005 1 1 1
;
data want;
set have;
array rr A B C;
length Reason $ 100;
do over rr;
if rr then Reason = catx(',', Reason, vname(rr));
end;
run;
Result:
Application_number A B C Reason 5002 1 1 0 A,B 5003 1 1 1 A,B,C 5004 0 0 1 C 5005 1 1 1 A,B,C
Works a treat @PeterClemmensen massive appreciation sent your way.
Massive appreciation appreciated 🙂
@danhopkinslewis wrote:
Hi,
Wonder if you can help. I'm trying to create a new variable which brings in variable names where they equal 1.
For example Reason is the variable I want to create.
Application_number A B C Reason 5002 1 1 0 A,B 5003 1 1 1 A,B,C 5004 0 0 1 C 5005 1 1 1 A,B,C
I've tried a VNAME but could only get it to bring in 1 hit. My data source has 49 variables that could have a possible 1.
I curious, what could you possibly do with a string such as one you are trying to create, if it has 49 different possible variables? What benefits would there be, compared to working with the 0/1 variables?
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.