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

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 774 views
  • 1 like
  • 4 in conversation