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 |
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;
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.
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?
These are known as Combinations and permutations
https://www.mathsisfun.com/combinatorics/combinations-permutations.html
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).
CALL ALLCOMB()
@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.
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;
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);
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.