Dear All,
I want to write a code to check every possible combination of variables for their values.
For example:
A B C D E F G ........
Yes Yes No Yes
No No Yes Yes
.......................................
For 7 variables with 2 selections, there are 21 combinations.
How can I write a code that if any two of A B C ..... G..... with "Yes", then the newvariable: new1 = "Yes", ie. A=Yes, D= Yes then new1 = "Yes". if A=No, D=Yes, then new1 = "No". ?
Thanks and Regards
Fred
Hi ... here's another idea, works no matter how many answers you have ...
data x;
input (answer1-answer4) (: $3.);
datalines;
Yes Yes No Yes
No No Yes Yes
No No No No
Yes Yes No No
Yes Yes Yes Yes
No Yes No No
;
run;
data x;
set x;
new = ifc(countc(catt(of answer:),'Y') ge 2, 'YES' , 'NO');
run;
proc print data=x noobs;
run;
answer1 answer2 answer3 answer4 new
Yes Yes No Yes YES
No No Yes Yes YES
No No No No NO
Yes Yes No No YES
Yes Yes Yes Yes YES
No Yes No No NO
fyi ... "Searching for Variable Values with CAT Functions: An Alternative to Arrays and Loops"
http://www.nesug.org/Proceedings/nesug09/ff/ff04.pdf
This is a case where binary (0/1) coding for no and yes make the task incredibly easy.
IF sum(of a--g)=2 then new1=1; ELSE new1=0;
This says if exactly 2 are yes, then set new1 to 1.
So you could recode a-g in an array and apply the logic above. Otherwise, you could write a DO loop to accomplish the same thing; something like
ARRAY vars a--g;
CountYes=0;
DO i=1 to DIM(vars);
IF vars{i}='Yes' THEN CountYes+1;
END;
IF CountYes=2 THEN new1='Yes'; ELSE new1='No';
Doc Muhlbaier
Duke
Hi ... here's another idea, works no matter how many answers you have ...
data x;
input (answer1-answer4) (: $3.);
datalines;
Yes Yes No Yes
No No Yes Yes
No No No No
Yes Yes No No
Yes Yes Yes Yes
No Yes No No
;
run;
data x;
set x;
new = ifc(countc(catt(of answer:),'Y') ge 2, 'YES' , 'NO');
run;
proc print data=x noobs;
run;
answer1 answer2 answer3 answer4 new
Yes Yes No Yes YES
No No Yes Yes YES
No No No No NO
Yes Yes No No YES
Yes Yes Yes Yes YES
No Yes No No NO
fyi ... "Searching for Variable Values with CAT Functions: An Alternative to Arrays and Loops"
http://www.nesug.org/Proceedings/nesug09/ff/ff04.pdf
Mike.
There is a problem. "catt(of answer:)" is limited by $200. ,so if OP has lots of variables ,you can not hold them all.
SO I think Doc@Duck 's method is good.
Ksharp
Ksharp,
Not so! According to the documentation:
The CATT function returns a value to a variable, or returns a value in a temporary buffer. The value that is returned from the CATT function has the following length:
Learned it. Art.T
I should check the documentation firstly before commenting something.
But I still insist that Doc@Duck 's code is better and more robust. Excuse me.
Regards!
Ksharp
Hi ... original posting mentioned 7 variables, plus as Art said ... the limit is the max length of a
character variable, 32K+.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.