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

Hi I have a table with a series of variables that have 1 or ' '  as values.  The variable are named as var1, var2, ..., var16.  If we just use var1-3 as example here, I want to create a new variable 'new' that has value of the variable name that has 1, but if a row has 2 or more variables with 1, then pick the smaller variable number, for example like this table:

 

Var1 Var2 Var3  new

1                  1     Var1

                    1     Var3

1        1        1     Var1

          1        1     Var2

 

 

Thank you.

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20
data have;
input Var1 Var2 Var3  ;
datalines;
1 . 1
. . 1
1 1 1  
. 1 1
;

data want;
set have;
array t(*) var:;
k=whichn(1, of t[*]);
new=vname(t(k));
drop k;
run;

View solution in original post

5 REPLIES 5
Reeza
Super User

Are your variable names var1, var2, var3 etc?

 

If so, look at the WHICHC/N functions which return the first index of the value you're looking for, assuming numeric it would look something like:

 

index = whichn(1, of var1-var16);
new=catt('Var', index);
sunless07652
Fluorite | Level 6

This is great.  Actually the variable are named TAPQ01-TAPQ16, so there's the 0 in front of 1-9, but I can just create a new set of variables named TAPQ1-TAPQ16, or just list them one by one.  Thank you.

Reeza
Super User

Z2 format will add 0 in front and it's not an issue with @novinosrin solution.

 

new=catt('Var', put(index, z2.));
novinosrin
Tourmaline | Level 20
data have;
input Var1 Var2 Var3  ;
datalines;
1 . 1
. . 1
1 1 1  
. 1 1
;

data want;
set have;
array t(*) var:;
k=whichn(1, of t[*]);
new=vname(t(k));
drop k;
run;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 2058 views
  • 6 likes
  • 3 in conversation