Hi all. I'm trying to create a variable (newvar) from six variables (var1, var2, var3, var4, var5, var6). Positive endorsement of any of the six will mean newvar=yes (even if it means positive endorsement of one and missing data for the other 5).
Currently, my code is : if var1=1 OR var2=1 OR var3=1 OR var4=1 OR var5=1 OR var6=1 THEN newvar=1;
If there is not a positive endorsement for any item and failure to answer all 6 items, then they must be considered missing. Negative endorsement of all six items will result in newvar=0. I'm seeking help for this code. Any suggestions will be appreciated.
Var1 | Var2 | Var3 | Var4 | Var5 | Var6 | newvar |
Yes | Missing | Missing | No | No | No | yes |
Missing | No | No | No | No | No | Missing |
No | No | No | No | No | No | No |
No | No | No | No | No | Yes | yes |
Since you're code is using 1 you have formats applied?
What do the actual values look like in your data?
You can look up the WHICHC/WHICHN which will search a list of variables to see if a particular values occurs and returns the index of the variable it does.
I'm pretty sure that's all you need here 🙂
New Variable from Multiple Variables
current code
if var1=1 OR var2=1 OR var3=1 OR var4=1 OR var5=1 OR var6=1 THEN newvar=1;
HAVE
Up to 40 obs WORK.HAVE total obs=10
Obs VAR1 VAR2 VAR3 VAR4 VAR5 VAR6
1 0 0 0 1 0 0
2 0 0 1 0 0 0
3 0 0 1 0 0 .
4 1 0 0 1 0 0
5 0 0 1 1 0 0
6 1 . 0 0 0 .
7 1 0 0 0 0 .
8 0 0 0 0 1 0
9 0 0 0 . 0 0
10 0 . 1 0 1 1
WANT
Up to 40 obs WORK.WANT total obs=10
Obs VAR1 VAR2 VAR3 VAR4 VAR5 VAR6 NEWVAR
1 0 0 0 1 0 0 1
2 0 0 1 0 0 0 1
3 0 0 1 0 0 . 1
4 1 0 0 1 0 0 1
5 0 0 1 1 0 0 1
6 1 . 0 0 0 . 1
7 1 0 0 0 0 . 1
8 0 0 0 0 1 0 1
9 0 0 0 . 0 0 0
10 0 . 1 0 1 1 1
WORKING CODE
newvar = (sum(of var:)>0);
* FULL SOLUTION;
* make data;
data have;
array sixpac var1-var6;
do i=1 to 10;
do over sixpac;
sixpac=(uniform(5733)>.8);
if (uniform(5731)>.95) then sixpac=.;
end;
output;
drop i;
end;
run;quit;
* SOLUTION;
data want;
set have;
newvar = (sum(of var:)>0);
run;quit;
Yes, I do have formats applied. It's survey data, so values are -9 (missing), 1(yes), and 2(no). I was not aware of the WHICHC/WHICHN, but will look into it.
Thanks!
Assumes 1=Yes, 0=No
if max(var1,var2,var3,var4,var5,var6)=1 then newvar=1;
else if n(var1,var2,var3,var4,var5,var6)=6 then newvar=0;
If one of the conditions is not true then newvar will be missing.
data have;
input (Var1
Var2
Var3
Var4
Var5
Var6
newvar) ($);
drop newvar;
cards;
Yes
Missing
Missing
No
No
No
yes
Missing
No
No
No
No
No
Missing
No
No
No
No
No
No
No
No
No
No
No
No
Yes
;
run;
data want;
set have;
array x{*} $ var:;
new='No ';
if 'Yes' in x then new='Yes';
else if 'Missing' in x then new='Missing';
run;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.