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,
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;
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;
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;
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;
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;
... learning everyday , thanks!
Also can be done in PROC FACTEX
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.