Solved
Contributor
Posts: 56

# variables combination

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

Accepted Solutions
Solution
‎08-23-2011 10:34 PM
Valued Guide
Posts: 765

## Re: variables combination

Hi ... here's another idea, works no matter how many answers you have ...

data x;

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;

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

All Replies
Posts: 2,125

## variables combination

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

Solution
‎08-23-2011 10:34 PM
Valued Guide
Posts: 765

## Re: variables combination

Hi ... here's another idea, works no matter how many answers you have ...

data x;

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;

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

Super User
Posts: 10,784

## Re: variables combination

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

PROC Star
Posts: 8,165

## Re: variables combination

Ksharp,

Not so! According to the documentation:

### Length of Returned Variable: Special Cases

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:

• up to 200 characters in WHERE clauses and in PROC SQL
• up to 32767 characters in the DATA step except in WHERE clauses
• up to 65534 characters when CATT is called from the macro processor
Super User
Posts: 10,784

## Re: variables combination

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

Valued Guide
Posts: 765

## Re: variables combination

Hi ... original posting mentioned 7 variables, plus as Art said ... the limit is the max length of a

character variable, 32K+.

🔒 This topic is solved and locked.