## macro for categorical variables using proc freq.

Solved
Occasional Contributor
Posts: 17

# macro for categorical variables using proc freq.

Hi Friends,

I  need all your help . I need a macro which can run all possible combinations for the N number of variables inside a proc freq.

For eg.

I have 5 variables. Say A B C D and E  all are binary . I need a 2 way classifcation of variables using proc freq. like it should generate

frequencies for AB AC AD AE BC BD BE CD and CE.

Now how i am doing is passing the values for each combination.

%macro freqproc freq data=chk;

table &var1*&var2;

run;

%mend freq;

%freq(var1=a , var2=b);

%freq(var1=a, var2=c);

.................................

................

All the 5 variables are binary. for eg. A has value 0 and 1, B has a value 0 and 1 and C....

So what i need an help on is creating frequency for the following combination
0 of A with 1 of B,

1 of A with 0 of B,

1 of A with 1 of B,

0 of A with 0 of B

the macro i am looking out for should do for all the A to E variables. the macro can be extendable to 3 ways..

Please let me know the thoughts,

Thanks and Regards,

Accepted Solutions
Solution
‎03-04-2013 04:46 AM
Regular Contributor
Posts: 151

## Re: macro for categorical variables using proc freq.

The data step with the CARDS statement was just used to create some dummy data to show the result of the PROC SUMMARY.  If you have many variables then use _numeric_ in the CLASS statement instead of listing them all (you should also drop any non required numeric variables). This will give you all the frequencies in a single table, you'd probably need some form of macro afterwards to transform the data to whatever format you want.

proc summary data=have (drop= ...);

class _numeric_;

ways 2;

output out=want (drop=_TYPE_);

run;

All Replies
Regular Contributor
Posts: 151

## Re: macro for categorical variables using proc freq.

I would be tempted to switch from PROC FREQ to PROC SUMMARY, that way you can calculate all 2 way (or 3 way) combinations in one go without using a macro.

data have;

input A B C D E;

cards;

0 1 1 0 1

1 0 0 1 0

1 1 1 1 1

0 0 0 0 0

;

run;

proc summary data=have;

class A B C D E;

ways 2;

output out=want (drop=_TYPE_);

run;

Occasional Contributor
Posts: 17

## Re: macro for categorical variables using proc freq.

Thank you Keith for the reply and suggestion. I have just gave an example of 5 variables. In actual i have aroun 200 variables. So what i thought that it will be difficult to use cards statement to list the values of 200 variables. thats what i was requesting for macro.

Thanks and Regards,

Solution
‎03-04-2013 04:46 AM
Regular Contributor
Posts: 151

## Re: macro for categorical variables using proc freq.

The data step with the CARDS statement was just used to create some dummy data to show the result of the PROC SUMMARY.  If you have many variables then use _numeric_ in the CLASS statement instead of listing them all (you should also drop any non required numeric variables). This will give you all the frequencies in a single table, you'd probably need some form of macro afterwards to transform the data to whatever format you want.

proc summary data=have (drop= ...);

class _numeric_;

ways 2;

output out=want (drop=_TYPE_);

run;

🔒 This topic is solved and locked.