New Contributor
Posts: 4

PROC FREQ with many variables

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!

PROC Star
Posts: 8,169

Re: PROC FREQ with many variables

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

New Contributor
Posts: 4

Re: PROC FREQ with many variables

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.

Super User
Posts: 9,604

Re: PROC FREQ with many variables

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.

New Contributor
Posts: 4

Re: PROC FREQ with many variables

``````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?

PROC Star
Posts: 8,169

Re: PROC FREQ with many variables

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

New Contributor
Posts: 4

Re: PROC FREQ with many variables

Thanks. It works very nicely.

Discussion stats
• 6 replies
• 121 views
• 0 likes
• 3 in conversation