BookmarkSubscribeRSS Feed
rotana
Calcite | Level 5

Does anyone know if there's an efficient way to create a single variable from multiple variables. For example, person was asked if they've ever had a condition, then asked to specify the condition. When the data was entered, each condition was listed as its own binary (yes/no) variable without standardizing the variable names. 

 

Condition         X           Y           Z

1                      1            0           0

1                      0            0           1

0                      0            0           0  

1                      0            1           0

 

I want it to look like this:

 

Condition         Condition_type

1                                  X

1                                  Z

0                                   

1                                  Y

 

Thanks!

 

3 REPLIES 3
Reeza
Super User

Use WHICHN to find the value of the one with the 1 and then VNAME to get the name of the variable:

 

array myVars(*) x y z;
index = whichn(1, of myVars(*));
if index ne 0 then VarName = vname(myvars(index));

@rotana wrote:

Does anyone know if there's an efficient way to create a single variable from multiple variables. For example, person was asked if they've ever had a condition, then asked to specify the condition. When the data was entered, each condition was listed as its own binary (yes/no) variable without standardizing the variable names. 

 

Condition         X           Y           Z

1                      1            0           0

1                      0            0           1

0                      0            0           0  

1                      0            1           0

 

I want it to look like this:

 

Condition         Condition_type

1                                  X

1                                  Z

0                                   

1                                  Y

 

Thanks!

 


 

novinosrin
Tourmaline | Level 20
data have;
input Condition         X           Y           Z;
cards;
1                      1            0           0
1                      0            0           1
0                      0            0           0  
1                      0            1           0
;


data want;
set have;
array t(*) x--z;
temp=whichn(1, of t[*]);
if  temp then condition_type=vname(t(temp));
drop temp;
run;
Astounding
PROC Star

Is it impossible for a person to have two conditions?

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

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