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?
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
Check out this tutorial series to learn how to build your own steps in SAS Studio.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.