BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Fred_Gavin
Calcite | Level 5

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

1 ACCEPTED SOLUTION

Accepted Solutions
MikeZdeb
Rhodochrosite | Level 12

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


View solution in original post

6 REPLIES 6
Doc_Duke
Rhodochrosite | Level 12

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

MikeZdeb
Rhodochrosite | Level 12

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


Ksharp
Super User

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

art297
Opal | Level 21

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

Ksharp
Super User

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

MikeZdeb
Rhodochrosite | Level 12

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

character variable, 32K+.

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!

What is Bayesian Analysis?

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 1616 views
  • 3 likes
  • 5 in conversation