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

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,

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

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

7 REPLIES 7
Reeza
Super User

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;
brulard
Pyrite | Level 9
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
PaigeMiller
Diamond | Level 26

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
brulard
Pyrite | Level 9

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 🙂 )

 

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;
PaigeMiller
Diamond | Level 26

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
brulard
Pyrite | Level 9

... learning everyday , thanks!

PaigeMiller
Diamond | Level 26

Also can be done in PROC FACTEX

--
Paige Miller

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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