BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
danhopkinslewis
Fluorite | Level 6

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_numberABCReason
5002110A,B
5003111A,B,C
5004001C
5005111A,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

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

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 

View solution in original post

4 REPLIES 4
PeterClemmensen
Tourmaline | Level 20

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 
danhopkinslewis
Fluorite | Level 6

Works a treat  @PeterClemmensen massive appreciation sent your way.

PeterClemmensen
Tourmaline | Level 20

Massive appreciation appreciated 🙂

PaigeMiller
Diamond | Level 26

@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?

--
Paige Miller

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

Creating Custom Steps in SAS Studio

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1088 views
  • 1 like
  • 3 in conversation