Hi,
Suppose I have p=3 categorical variables A, B, C and I would like frequency tables for each pair: A*B, A*C, B*C. Of course, I can run 3 PROC FREQ. But if I have p=10, there will be many more PROC FREQ. On the other hand, the following code can generate additional tables for A*A, B*B and C*C, which are not needed.
PROC FREQ
TABLES (A B C)*(A B C)
Question: how to do this efficiently and generate tables only needed?
Thanks!
Couldn't you just use something like:
proc freq data=sashelp.cars;
tables make * (type origin drivetrain);
tables type * (origin drivetrain);
tables origin*drivetrain;
run;
Art, CEO, AnalystFinder.com
Sure, but that is not the most efficient way. What if I have 10000 variables? Of course, you can write 10000 lines. What I am asking is something more efficient than that.
Why would you have 10k variables. Sorry, this is something which comes up 3 or 4 times a day. Transposed data makes your coding life harder. Keep your data in long form - this way you can have any number of items, and number of variables is really small and easy to manage. SAS is not Excel! If you provide some test data in the form of a datastep then we can come up with some code.
proc freq data=sashelp.cars;
tables make * (type origin drivetrain);
tables type * (origin drivetrain);
tables origin*drivetrain;
run;
In the above example posted by art297, there are 3 tables statement. Alternative, how to do this in just one table statement and achieve the same exactly results?
Here is an alternative. It wouldn't work for 10,000 variables, but would work if the total number of characters needed for the tables statement was less than 64K:
data vars;
input var1 $32.;
cards;
make
type
origin
drivetrain
;
proc sql noprint;
select catx(' ','tables',a.var1,'*',b.var2,';')
into : tables separated by ' '
from vars a, vars (rename=(var1=var2)) b
where var1 ne var2 and
var1 gt var2
;
quit;
proc freq data=sashelp.cars;
&tables.;
run;
Art, CEO, AnalystFinder.com
Thanks. It works very nicely.
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.