Link to home
Start Free TrialLog in
Avatar of labradorchik
labradorchikFlag for United States of America

asked on

How to create frequencies of a variable from SAS dataset?

Hello,
I am trying to create frequencies of var1 including missing values from data1 SAS dataset.  Is there a different way of doing this or a better/more efficient way?
Please see my code below:

proc freq data=lib.data1;
 tables var1 / missing;
 title "Frequency Report of var1";
run;

Open in new window

Avatar of Aloysius Low
Aloysius Low
Flag of Singapore image

It depends on what you wish to achieve, but i would say that's as good as you can get.. there are many paths to achieve what you want..

you can also use PROC SQL with a group by and count function you'll get the same result as well, only that you don't get the cumulative frequency information..

the other way is to sort the data and and use a data step to count while you process row by row..

if the data set is small, there really isn't much difference
Avatar of labradorchik

ASKER

Thank you, lowaloysius!
dataset that I am using has a few 100,000 records but it runs pretty fast even with the current above code. I have 2 (PROC FREQ) frequency codes exactly as the one above just with different variables. Can I somehow place these frequency reports tables in a report file generated upon the invocation of these 2 PROC FREQ SAS codes?

For example: right now I am only seeing my frequency tables on the Output screen but I would like for these two PROC FREQ codes to generate these frequency tables in an output file. How can I do that?
on the tables line, after the MISSING keyword before the semi-colon, add OUT = [output dataset name]

this will output the frequency table to a SAS data set
Thank you!
What if want to place these 2 frequency tables as well as their titles in frequency.lst file?

When I tested it looks like .lst file does not work with the OUT statement.  

Here is my current code:

libname lib 'C':\';

%macro FREQCODE;

proc freq data=lib.data1;
 tables var1 / missing out=lib.frequency.lst;
 title "Frequency Report of var1";
run;

proc freq data=lib.data2;
 tables var5 / missing out=lib.frequency.lst;
 title "Frequency Report of var5";
run;

%mend FREQCODE;
%FREQCODE;

Open in new window

i suppose you want to see what you are seeing in Enterprise Guide or base SAS created into a file? if so it's not possible to do directly.

i've not tried before, but you probably need to do an ODS to output into a file and put the PROC FREQ in between. otherwise, try to do a PROC PRINT on the output SAS dataset between the same ODS statement.. that should work (again, no environment to test)..
Thank you for your comments!
Could you please explain what do you mean by "to do an ODS to output into a file and put the PROC FREQ in between"?
ASKER CERTIFIED SOLUTION
Avatar of Aloysius Low
Aloysius Low
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you very much!!
I am still testing your code and looks more efficient what I currently have right now. This is what I have currently:    

libname lib 'C':\';

%macro FREQCODE;

proc freq data=lib.data1;
 tables var1 / missing out=lib.freqdata1;
 title "Frequency Report of var1";
run;

proc printto print=lib.frequency.lst new;
run;

proc print data=lib.freqdata1;
 Title 'Frequency Report of var1';
run;

proc printto;
run;

proc freq data=lib.data2;
 tables var5 / missing out=lib.freqdata2;
 title "Frequency Report of var5";
run;

proc printto print=lib.frequency.lst;
run;

proc print data=lib.freqdata2;
 Title 'Frequency Report of var5';
run;

proc printto;
run;

%mend FREQCODE;
%FREQCODE;

Open in new window


The only problem with this code is that lib.frequency.lst is created very strange. It created .lst file in the frequency folder under the lib directory and I wanted my file name to be frequency.lst
i'm not seeing what you are seeing for the lib.frequency.lst based on your description.

In any case, if you are referencing [library].[sas name] reference, it should be referencing a dataset or catalog, not a filename itself.
Hi lowaloysius! I am sorry, I thought I closed this question before but I didn't.  Thank you very much for your  comment!