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

Hi All,

 

I'm fairly confident this is an easy ask.  Let's assume i have 4 variables A through D.  Each variable is populated with a 1 or is missing (blank).  How can i generate a data set that contains all possible combinations, taking into account the missing (or stated differently, is cumulative beginning by listing a combination with only a single variable value, and so on).

 

So, given my scenario above, i know there are 15 possible profiles/combinations (shown below in the table) and based on summing acorss the combination formula when the total=1 and chosen =1 (result=4), 2 (result=6), 3 (result=4) and 4 (result=1).  Can i use PROC PLAN to accomplish this?  I've tried unsuccessfully...

 

A

B

C

D

1

 

 

 

 

1

 

 

 

 

1

 

 

 

 

1

1

1

 

 

1

 

1

 

1

 

 

1

1

1

1

 

1

1

 

1

1

1

1

 

1

1

1

1

 

1

1

 

 

1

 

1

 

1

1

1

 

 

1

1

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data have;
 array x{4} a b c d;
 n=dim(x);
 k=-1;
 total=2**n;
 do i=1 to total;
  rc=graycode(k,of x{*});
  output;
 end;
 keep a b c d;
 run;

View solution in original post

8 REPLIES 8
ballardw
Super User

one way:

 

data want;
   do a = 1, .;
   do b = 1, .; 
   do c = 1, .;
   do d = 1, .;
   output;
   end;
   end;
   end;
   end;
run;

If you need a specific order that is a significantly different question.

 

And there are 16 combinations: you missed all missing.

jsberger
Obsidian | Level 7

Thanks...i knew it was going to be easy for someone.

 

Out of curiosity...is there a formula that can be used to calculate the number (as corrected 16) in this type of scenario?

Reeza
Super User

These are known as Combinations and permutations

https://www.mathsisfun.com/combinatorics/combinations-permutations.html

jsberger
Obsidian | Level 7

I searched for a solution within permutations and combinations to no avail.  I am only interested in combinations...but i don't want to require an element from each of all 4.  I could not find a solution where SAS PROC PLAN would output all the combinations taking into account each variable with 2 possible values (either 1 or 0).

Reeza
Super User

CALL ALLCOMB()

 

ballardw
Super User

@jsberger wrote:

Thanks...i knew it was going to be easy for someone.

 

Out of curiosity...is there a formula that can be used to calculate the number (as corrected 16) in this type of scenario?


If you have k number of variables and all of them have the same n number of levels then the number is n**k  (n to the k-th power).

Your example: 4 variables all 2 levels = 2**4 = 16.

If they do not have all the same number of levels then it is the product of the number of levels of each variable: Suppose you have 3 variables with 2 4 and 6 levels respectively: 2*4*6 combinations.

 

 

Ksharp
Super User
data have;
 array x{4} a b c d;
 n=dim(x);
 k=-1;
 total=2**n;
 do i=1 to total;
  rc=graycode(k,of x{*});
  output;
 end;
 keep a b c d;
 run;

jsberger
Obsidian | Level 7

Kevin,

 

Thanks!  I like this...very slick and easily wrapped in a macro for quick calculation:

%macro comb_list(num);
data comb_list;
array vec{&num} var1-var#
k=-1;
total=2**#
do i=1 to total;
rc=graycode(k,of vec{*});
output;
end;
keep var:;
run;
%mend;
%comb_list(num=7);

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
What is ANOVA?

ANOVA, or Analysis Of Variance, is used to compare the averages or means of two or more populations to better understand how they differ. Watch this tutorial for more.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 8 replies
  • 1960 views
  • 0 likes
  • 4 in conversation