Solved
Contributor
Posts: 44

# proc frequency outputs

proc freq data=xx;

tables var1-var20 / out=varoutputs;

run;

we are getting output for var20 only. how to get output for all the 20 varibles.

if we need to write 20 tables statements, how to write in a short cut?

proc freq data=learn.survey;

tables 'Ques' || i  /  out='outque' || i;

run;

Accepted Solutions
Solution
‎10-08-2014 06:08 AM
Contributor
Posts: 21

## Re: proc frequency outputs

Dear Rajesh, I totally agree with Jaap. Please try to run below code and see if this helps.

proc sql noprint;

select distinct(name) into :var separated by ' ' from dictionary.columns

where upcase(libname) eq 'LEARN' and upcase(memname) eq 'SURVEY';

quit;

%let cnt = %eval(%sysfunc(count(&var., %str( ))) + 1);

%macro aaa;

proc freq data = aaa noprint;

%do i = 1 %to &cnt.;

tables %scan(&var., &i.) / out = outque&i.;

%end;

run;

%mend; %aaa;

All Replies
Posts: 3,215

## Re: proc frequency outputs

It is doing with the out dataset exactly what you coded and what is documented wiht that.  The last variable will be in that dataset/

Base SAS(R) 9.4 Procedures Guide: Statistical Procedures, Second Edition (proc freq statement). Always nice to see the documentation.

You will need to repeat the tables var-  / out= ; line.

---->-- ja karman --<-----
Solution
‎10-08-2014 06:08 AM
Contributor
Posts: 21

## Re: proc frequency outputs

Dear Rajesh, I totally agree with Jaap. Please try to run below code and see if this helps.

proc sql noprint;

select distinct(name) into :var separated by ' ' from dictionary.columns

where upcase(libname) eq 'LEARN' and upcase(memname) eq 'SURVEY';

quit;

%let cnt = %eval(%sysfunc(count(&var., %str( ))) + 1);

%macro aaa;

proc freq data = aaa noprint;

%do i = 1 %to &cnt.;

tables %scan(&var., &i.) / out = outque&i.;

%end;

run;

%mend; %aaa;

Posts: 1,147

## Re: proc frequency outputs

hi rajesh, please try another way by call execute

data class;

set sashelp.class;

run;

data _null_;

set sashelp.vcolumn(where=(libname='WORK' and memname="CLASS") keep=libname memname name varnum);

call execute(

'proc freq data='||memname||';

tables '|| name|| '/ out='||cats('outque',put(varnum,1.))||';

run;');

run;

Hope it helps.

Thanks,

Jag

Thanks,
Jag
Super User
Posts: 10,784

## Re: proc frequency outputs

for you first question. use ODS .

```data class;set sashelp.class;run;

ods select OneWayFreqs;
ods output OneWayFreqs=want;
proc freq data=class ;
tables age sex /nocum norow nocol nopercent;
run;

```

Xia Keshan

Contributor
Posts: 44

## Re: proc frequency outputs

hi ,

sorry to say in this way. I did not expect this kind of answer from you.

did you run this and observed the output of want

Super User
Posts: 10,784

## Re: proc frequency outputs

Did you run my code ?  and my code is for  the question -- "how to get output for all the 20 varibles."

New Contributor
Posts: 4

## Re: proc frequency outputs

Hi Rajeshm,

You do this for all the 20 variables but at each step, you call your output table "varoutputs"

SAS do it for VAR1 and create varoutputs

SAS do it for var2 and create varoutputs for var2. This will replace varoutputs that already exist

at the end, you only have varoutputs corresponding to the last step: var20.

If you want 20 output you have to call them with different name.

That is why you need to do a loop:

in order to do a loop, you need to be inside a macro.

The easiest way to do what you want to do, for me, would be:

%Macro varoutputs();

%Do i=1 %to 20 %do;

proc freq data=xx;

tables var&i. / out=varoutputs&i.;

run;

%end;

%Mend;

%varoutputs();

Posts: 3,852

## Re: proc frequency outputs

Actually the output from program is what you want.  Or at least what you should want; one data set with all the one-way tables stacked together, with an information variable TABLE.

No need for macro loops, call execute, what have you.

proc freq data=sashelp.class ;
ods select none;

ods output OneWayFreqs=want;
tables age sex /nocum norow nocol nopercent;

run;
ods select all;
proc print;

run;

Posts: 3,215

## Re: proc frequency outputs

I do not know what the OP 's intentions was. The output included by data_null_ is showing the logical inconsistency of the question.
Different variables different values to count  (Age Sex) it is solved by Xia's way by adding missing when appending those data of different tables.

---->-- ja karman --<-----
🔒 This topic is solved and locked.