Hello
Lets say I have 10 numbers and I want to choose 6 of them .
There are 210 possible combinations of choose 6 numbers from 10.(select a subset of 6 elements from a set of 10).
I want to get the all 210 combinations
So in want data set will have columns: serial X1 X2 X3 X4 X5 X6
what is the way to do it please?
%let numbers=2,4,16,21,22,24,27,31,32,34;
Data want;
input serial X1 X2 X3 X4 X5 X6;
cards;
1 4 16 21 22 24
2 16 21 22 24 27
and so on
;
Run;
You can use the COMB and ALLCOMB functions to do this. I have modified your code below.
data test;
array x[10] (2,4,16,21,22,24,27,31,32,34);
n=dim(x);
k=6;
ncomb=comb(n, k);
do j=1 to ncomb+1;
rc=allcomb(j, k, of x[*]);
put j 5. +3 x1-x6 +3 rc=;
end;
run;
Yes. The most convenient way is using ALLCOMB() function.
But if you would like to perform it on your own. Here you go.
%let numbers=2,4,16,21,22,24,27,31,32,34;
Data want;
array x{10} _temporary_ (&numbers);
do a1=1 to 10;
do a2=a1+1 to 10;
do a3=a2+1 to 10;
do a4=a3+1 to 10;
do a5=a4+1 to 10;
do a6=a5+1 to 10;
serial+1;
x1=x{a1};
x2=x{a2};
x3=x{a3};
x4=x{a4};
x5=x{a5};
x6=x{a6};
output;
end;
end;
end;
end;
end;
end;
drop a:;
Run;
And Here is a SQL solution.
data x; infile cards dlm=','; input x @@; cards; 2,4,16,21,22,24,27,31,32,34 ; run; ods select none; ods output sql_results=want; proc sql number; select x1.x as x1, x2.x as x2, x3.x as x3, x4.x as x4, x5.x as x5, x6.x as x6 from x as x1, x as x2, x as x3, x as x4, x as x5, x as x6 where x1.x<x2.x and x2.x<x3.x and x3.x<x4.x and x4.x<x5.x and x5.x<x6.x ; quit; ods select all;
Since others have given you a DATA step and PROC SQL solution, here is the PROC IML solution:
%let numbers=2,4,16,21,22,24,27,31,32,34;
proc iml;
x = {&numbers};
idx = allcomb(nrow(x), 6);
comb = shape(x[idx,], nrow(idx));
/* then print or save to data set */
To finish the Rick's IML code with creating a WANT table:
%let numbers=2,4,16,21,22,24,27,31,32,34;
proc iml;
x = {&numbers};
idx = allcomb(nrow(x), 6);
comb = shape(x[idx,], nrow(idx));
/* then print or save to data set */
create want from comb[c=('x1':'x6')];
append from comb;
close;
quit;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.