Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving results from SAS proc freq with multiple tables

Tags:

sas

sas-ods

I'm a beginner in SAS and I have the following problem.

I need to calculate counts and percents of several variables (A B C) from one dataset and save the results to another dataset. my code is:

proc freq data=mydata; tables A B C / out=data_out ; run;

the result of the procedure for each variable appears in the SAS output window, but data_out contains the results only for the last variable. How to save them all in data_out? Any help is appreciated.

like image 681
Natalia Avatar asked Jan 16 '15 20:01

Natalia


People also ask

How do you save a frequency table in SAS?

If you want to save the results as a SAS data set, you can use the ODS OUTPUT statement and save the data set to a libref that points to a directory on your PC.

How do you get Proc FREQ output in a dataset?

PROC FREQ produces two types of output data sets that you can use with other statistical and reporting procedures. You can request these data sets as follows: Specify the OUT= option in a TABLES statement. This creates an output data set that contains frequency or crosstabulation table counts and percentages.

What is the difference between PROC FREQ and Proc Tabulate?

Proc tabulate is predominately used to make nice looking tables. Unlike proc freq this procedure can handle multiple variables in the row and column expressions. It can also handle multiple levels in both rows and columns whereas proc freq will only create two variable contingency tables.

What does the TABLES Statement do in the PROC FREQ step?

The TABLES statement requests one-way to n-way frequency and crosstabulation tables and statistics for those tables. If you omit the TABLES statement, PROC FREQ generates one-way frequency tables for all data set variables that are not listed in the other statements.


2 Answers

ODS OUTPUT is your answer. You can't output directly using the OUT=, but you can output them like so:

ods output OneWayFreqs=freqs;
proc freq data=sashelp.class;
  tables age height weight;
run;
ods output close;

OneWayFreqs is the one-way tables, (n>1)-way tables are CrossTabFreqs:

ods output CrossTabFreqs=freqs;
ods trace on;
proc freq data=sashelp.class;
  tables age*height*weight;
run;
ods output close;

You can find out the correct name by running ods trace on; and then running your initial proc whatever (to the screen); it will tell you the names of the output in the log. (ods trace off; when you get tired of seeing it.)

like image 199
Joe Avatar answered Sep 28 '22 03:09

Joe


Lots of good basic sas stuff to learn here

1) Run three proc freq statements (one for each variable a b c) with a different output dataset name so the datasets are not over written.

2) use a rename option on the out = statement to change the count and percent variables for when you combine the datasets

3) sort by category and merge all datasets together

(I'm assuming there are values that appear in in multiple variables, if not you could just stack the data sets)

data mydata;
    input a $ b $ c$;
    datalines;
r r g
g r b
b b r
r r r
g g b
b r r
;
run;

proc freq noprint data = mydata; 
    tables a / out = data_a 
    (rename = (a = category count = count_a percent = percent_a)); 
run;
proc freq noprint data = mydata; 
    tables b / out = data_b 
    (rename = (b = category count = count_b percent = percent_b)); 
run;
proc freq noprint data = mydata; 
    tables c / out = data_c 
    (rename = (c = category count = count_c percent = percent_c)); 
run;

proc sort data = data_a; by category; run;
proc sort data = data_b; by category; run;
proc sort data = data_c; by category; run;

data data_out;
    merge data_a data_b data_c;
    by category;
run;
like image 28
KnowYourOnion Avatar answered Sep 28 '22 02:09

KnowYourOnion