New Variable from Multiple Variables

Reply
New Contributor
Posts: 2

New Variable from Multiple Variables

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

Super User
Posts: 17,730

Re: New Variable from Multiple Variables

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 Smiley Happy

Valued Guide
Posts: 505

Re: New Variable from Multiple Variables

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;




New Contributor
Posts: 2

Re: New Variable from Multiple Variables

[ Edited ]

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!

Super User
Posts: 10,458

Re: New Variable from Multiple Variables

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.

Super User
Posts: 9,662

Re: New Variable from Multiple Variables

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;
Ask a Question
Discussion stats
  • 5 replies
  • 136 views
  • 0 likes
  • 5 in conversation