Generate all permutations list - 2 levels

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 123
Accepted Solution

Generate all permutations list - 2 levels

hi,

 

Can someone suggest an approach to list all permutations for:

 

Categories

R

C

U

 

and possible level is either (char) 1, 2, or 3

 

where each outputted row would contain each category with its possible level such as a sample of full output would look like:

 

Possibility

Permutations

1

R1-C1-U1

2

R1-C1-U2

3

R1-C2-U2

4

R1-C2-U3

5

R2-C2-U2

ETC.

 

thanks in advance,

 

 


Accepted Solutions
Solution
‎05-08-2018 03:40 PM
Respected Advisor
Posts: 3,067

Re: Generate all permutations list - 2 levels

[ Edited ]

Much simpler:

 

data r;
    input r $;
    cards;
R1
R2
R3
;
run;
 /* repeat for data sets C and U */
proc sql;
    select cats(r,c,u) from r,c,u;
    /* ALTERNATIVE: select catx('-',r,c,u) from r,c,u; */
quit;

 

 

--
Paige Miller

View solution in original post


All Replies
Super User
Posts: 23,778

Re: Generate all permutations list - 2 levels

If you have IML this becomes trivial, see the example here:

https://blogs.sas.com/content/iml/2013/09/30/generate-combinations-in-sas.html

 

If you don't have IML you can use CALL ALLCOMB or you do a SQL join. 

 

data have;
input name $;
cards;
A
B
C
;
run;

proc sql;
create table want as
select catx("-", t1.name, t2.name, t3.name) as want
from have as t1
cross join have as t2
cross join have as t3;
quit;
Frequent Contributor
Posts: 123

Re: Generate all permutations list - 2 levels

Thanks for the tip, hi, i could consider IML in Unix (aix) sas as it is running. I'll read this link to learn how to output it to a dataset https://blogs.sas.com/content/iml/2011/04/18/writing-data-from-a-matrix-to-a-sas-data-set.html
Respected Advisor
Posts: 3,067

Re: Generate all permutations list - 2 levels

Also trivial using Cartesian join in PROC SQL

 

data r;
    input r;
    cards;
1
2
;
run;

/* Repeat to create data sets C and U */

proc sql;
    create table want as select * from r,c,u;
quit;
--
Paige Miller
Frequent Contributor
Posts: 123

Re: Generate all permutations list - 2 levels

Posted in reply to PaigeMiller

thanks for ur tip to create 3 tables... In order to ultimately arrive to 1 column output table, had to add a few lines . (there is likely a more efficient way of doing it Smiley Happy )

 

data r;
    input r0;
    cards;
1
2
3
;
run;

/* Repeat to create data sets C and U */

data c;
    input c0;
    cards;
1
2
3
;
run;

data u;
    input u0;
    cards;
1
2
3
;
run;

proc sql;
    create table want as select * from r,c,u;
quit;

/*convert to char var */

data want2;
format r c u $1.;
set want;
r=left(put(r0,best.));
c=left(put(c0,best.));
u=left(put(u0,best.));
run;

/* use trim with char vars  to produce output as one column */

proc sql; create table wantFIN as 
select trim('R'||r||'C'||c||'U'||u) as NEWVAR
from want2;
quit;
Solution
‎05-08-2018 03:40 PM
Respected Advisor
Posts: 3,067

Re: Generate all permutations list - 2 levels

[ Edited ]

Much simpler:

 

data r;
    input r $;
    cards;
R1
R2
R3
;
run;
 /* repeat for data sets C and U */
proc sql;
    select cats(r,c,u) from r,c,u;
    /* ALTERNATIVE: select catx('-',r,c,u) from r,c,u; */
quit;

 

 

--
Paige Miller
Frequent Contributor
Posts: 123

Re: Generate all permutations list - 2 levels

Posted in reply to PaigeMiller

... learning everyday , thanks!

Respected Advisor
Posts: 3,067

Re: Generate all permutations list - 2 levels

Also can be done in PROC FACTEX

--
Paige Miller
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 145 views
  • 4 likes
  • 3 in conversation