BookmarkSubscribeRSS Feed
John927
Calcite | Level 5

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!

6 REPLIES 6
art297
Opal | Level 21

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

 

John927
Calcite | Level 5

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.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

John927
Calcite | Level 5
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? 

art297
Opal | Level 21

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

 

John927
Calcite | Level 5

Thanks. It works very nicely.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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