Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to use Array to get all combinations of 10 fields' values?

Status
Not open for further replies.

iren

Technical User
Mar 8, 2005
106
US
Hi,

I have 20 fields in my dataset of 60 observations. . 10 fields in my dataset have Yes or No value and I need to generate all possible combinations of the 10 fields values.

ID
.
.
.

MyField1=”Yes”/”No”
MyField2=”Yes”/”No”
MyField3=”Yes”/”No”
MyField4=”Yes”/”No”
MyField5=”Yes”/No”
MyField6=”Yes”/No”
MyField7=”Yes”/No”
MyField8=”Yes”/No”
MyField9=”Yes”/No”
MyField10=”Yes”/No”

For ex.
MyField1=”Yes”
MyField2=”Yes”
MyField3=”Yes” AND
MyField10=”No”


I have a feeling that Array is needed for that purpose…However I have no idea in what way to implement it. Actually I never handled with arrays ?

Any help would be greatly appreciated

Thanks,

Iren
 
I'm a bit rusty in my maths, but if I remember correctly, the number of possible combinations is 2 to the power of 10 which is 1024, which is a fair bit of data to generate. What exactly are you trying to do? Sometimes in these situations it is better to take a step back and analyse what is is you are trying to achieve, there might be an easier way.
 
Chris,

I am trying to create combinations of responses to evaluate the condition .

As you can see below I tried to use proc Freq and I find 10 combinations, but as you noted there might be a lot of them!
I do not see any other way to do it except ARRAY....however I do not know how...

Iren




libname d 'c:\TEST\SAS Datasets';

/* Create dataset called Indicate which contains members whose rows all agree with the combinations of factors

Create multiple combinations :
1. FIRST = Y for rows where all specific questions meet YES RESPONSE
2. SECOND = Y for rows that meet 2 response positive
3. THIRD = Y for rows where member responses for 3 questions are answered positively
4. FOURTH=y for rows where member responses for 4 questions are answered positively
5. FIFTH=Y for rows where member responses for 5 questions are answered positively
6. SIXTH=Y for rows where member responses for 6 questions are answered positively
7. SEVENTH=Y rows where member responses for 7 questions are answered positively
8. EIGHTH=Y for rows where member responses for 8 questions are answered positively
9. NINETH=Y for rows where member responses for 9 questions are answered positively
10. TENTH=Y for rows where member responses for 10 questions are answered positively
*/

data Indicate;
set D.myOutput;


/*FIRST*/
if FIELD1 ='Yes' AND
FIELD2 ='Yes' AND
FIELD3='Yes' AND
FIELD4='Yes' AND
FIELD5='Yes' AND
FIELD6='Yes' AND
FIELD7='Yes' AND
FIELD8='Yes' AND
FIELD9='Yes' AND
FIELD10='Yes' THEN
FIRST = 'Y';
else FIRST = 'N';

/*SECOND*/
if FIELD1 ='Yes' AND
FIELD2 ='Yes' THEN

SECOND='Y';
else SECOND= 'N';

/*THIRD*/

if FIELD1 ='Yes' AND
FIELD2 ='Yes' AND
FIELD3='Yes' THEN

THIRD='Y';
else THIRD= 'N';

/*FOURTH*/

if FIELD1 ='Yes' AND
FIELD2 ='Yes' AND
FIELD3='Yes' AND
FIELD4='Yes' THEN

FOURTH='Y';
else FOURTH= 'N';


/*FIFTH*/

if FIELD1 ='Yes' AND
FIELD2 ='Yes' AND
FIELD3='Yes' AND
FIELD4='Yes' AND
FIELD5='Yes' THEN

FIFTH='Y';
else FIFTH= 'N';

/*SIXTH*/


if FIELD1 ='Yes' AND
FIELD2 ='Yes' AND
FIELD3 ='Yes' AND
FIELD4='Yes' AND
FIELD5='Yes' AND
FIELD6='Yes' THEN

SIXTH='Y';
else SIXTH= 'N';

/*SEVENTH*/

if FIELD1 ='Yes' AND
FIELD2 ='Yes' AND
FIELD3='Yes' AND
FIELD4='Yes' AND
FIELD5='Yes' AND
FIELD6='Yes' AND
FIELD7='Yes' THEN

SEVENTH='Y';
else SEVENTH= 'N';


/*EIGHTH*/

if FIELD1 ='Yes' AND
FIELD2 ='Yes' AND
FIELD3='Yes' AND
FIELD4='Yes' AND
FIELD5='Yes' AND
FIELD6='Yes' AND
FIELD7='Yes' AND
FIELD8='Yes' THEN

EIGHTH='Y';
else EIGHTH='N';

/*NINETH*/

if FIELD1 ='Yes' AND
FIELD2 ='Yes' AND
FIELD3='Yes' AND
FIELD4='Yes' AND
FIELD5='Yes' AND
FIELD6='Yes' AND
FIELD7='Yes' AND
FIELD8='Yes' AND
FIELD9='Yes' THEN

NINETH='Y';
else NINETH='N';

/*TENTH*/

if FIELD1 ='Yes' AND
FIELD2 ='Yes' AND
FIELD3='Yes' AND
FIELD4='Yes' AND
FIELD5='Yes' AND
FIELD6='Yes' AND
FIELD7='Yes' AND
FIELD8='Yes' AND
FIELD9='Yes' AND
FIELD10='Yes' THEN

TENTH='Y';
else TENTH='N';

run;


proc sort data=Indicate;
by member_id;
run;

proc freq data=INDICATE;
tables FIRST SECOND THIRD FOURTH FIFTH
SIXTH SEVENTH EIGHTTH NINETH TENTH;
title 'Frequency of variables in THE Audit';

run;

 
Are you trying to set up test data, or check what combination has been read in from your data source? That's what I meant by take a step back, rather than trying to solve this component of your problem, if we try to understand exactly what the desired final result is, we might see an entirely different way of solving it.

From what I see, you have got data containing Yes or No answers to 10 questions, and you're wanting to handle these by categorising them. First of all, your code have a hole in it. When testing your conditions, if TENTH is true, then so NINTH, EIGHTH, SEVENTH etc, you need to look at the logic there a bit more carefully. Maybe that is what you want, I don't know.
Now, an easy way to work with this information to uniquely identify each of the 1024 possible combinations, is to reduce it to a number. Create 10 new columns, 1 for each Field, then if FIELD1 is "Yes", you set your NEW_FIELD1 to "1" and so on for each field. Then, concatenate these 1s and 0s together. This will give you a 10 digit binary number, which will give you a number up to 1023 (0 being the 1024th number). You can use a SAS function to read this in as a binary number, and the value will uniquely identify a combination of answers.

If this doesn't answer your problem, you'll need to explain what it is you are trying to achieve (ie the business requirement you are trying to fulfill with your code).
 
Chris,

…I am trying to check (not set up) combinations results.

You were right saying that I needed to clarify what I needed. Actually I do not need all possible combinations. I just need certain combinations ( specified in the code which I sent to you).
In other word :
For 10 fields
For 9 fields
For 8 fields
For 7 fields
For 6 fields
For 5 fields
Etc

My output (From proc Freq) looks like the followings:

FIRST Frequency Percent
_______________________
N 56 100


SECOND Frequency Percent
_______________________
N 51 91.07
Y 5 8.93


THIRD Frequency Percent
__________________________
N 55 98.21
Y 1 1.79
.
.

TENTH Frequency Percent
_______________________
N 56 100
However I would need the same thing implemented more efficiently because there are too many lines in my code.Although at this time I have just 10 vars but potentially there might be much more fields.

What do you think?

Thank You,

Iren
 
Looking at the code you've put up there, if "First" is true, then so will Second, Third, Fourth etc. You may also need to confirm that the other fields are not "Yes" as well to distinguish between the different values.
A way to reduce the number of lines of code would be to concatenate your fields together, and just keep the first letter of each field. For instance:-
Code:
 length nfield1-nfield10 $1 allfields $10;
 nfield1 = substr(nfield1,1,1);
 nfield2 = substr(nfield2,1,1);
 etc....

 allfields = nfield1 || nfield2 || nfield3 .....;

 select(allfields);
     when('YYYYYYYYYY') FIRST='Y';
     when('YYNNNNNNNN') SECOND='Y';
     ....
     OTHERWISE ......;
 end;

Should simplify things a little for you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top