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?
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.