turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- General Programming
- /
- PROC FREQ with many variables

Topic Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

05-04-2018 09:35 AM

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!

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to John927

05-04-2018 09:57 AM

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

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to art297

05-04-2018 10:03 AM

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.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to John927

05-04-2018 10:08 AM

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.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

05-04-2018 10:22 AM

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

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to John927

05-04-2018 10:35 AM

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

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to art297

05-04-2018 11:24 AM

Thanks. It works very nicely.