How to export selected p-values to the table in SAS? - sas

I'm trying to write a program in SAS that supports decision-making process in selecting the best formula of linear regression model. I even had one but in R environment. Now I have to implement it in SAS. The final result should be a dataset with each line decribing different regression formulas i.e. names of explanatory variables, R-squared, p-values for different statistical tests, etc.
As an example, one of the tests is Durbin-Watson test for autocorrelation. My goal is to insert a p-value into the table I've mentioned. I use the code:
proc reg data=indata outest=outdata EDF ridge=0 OUTVIF;
model PKB = PK INV SI / DW;
run;
quit;
And as a result I get in the output window:
Durbin-Watson Statistics
Order DW Pr < DW Pr > DW
1 1.2512 0.0038 0.9962
I want to insert those p-values directly into the SAS table. I tried to find an answer in the SAS OnlineDoc and on the forum but with no success.

ODS OUTPUT is the best way to get information that you can print to the screen into datasets. Use ODS TRACE ON; before your code, run it, then inspect the log; see what table name matches what you're looking for. Then use ODS OUTPUT <tablename>=<datasetname>.
For example, in this PROC FREQ, I see ONEWAYFREQS is the table I want.
ods trace on;
proc freq data=sashelp.class;
var age;
run;
ods trace off;
So I use ODS OUTPUT:
ods output onewayfreqs=ages;
proc freq data=sashelp.class;
table age;
run;
ods output close;
and get a nice dataset. (ODS TRACE is not necessary if you know the name of the table you're looking for.)

Related

Store the value generated by PROC FREQ

The PROC Freq function generates a frequency table with the percentage, and the frequency of the variables. Is there a way to store the percentage of the variable for later use (like use this value to create dataset, create graphs)?
ODS OUTPUT is your friend any time you want to take the results of a PROC. Many procs, including PROC FREQ, also have output options built-in, but the generic one is ODS OUTPUT.
First, use ODS TRACE to identify the (internal to SAS) name of the table you need.
ods trace on;
proc freq data=sashelp.class;
tables age;
run;
ods trace off;
This generates:
Output Added:
-------------
Name: OneWayFreqs
Label: One-Way Frequencies
Template: Base.Freq.OneWayFreqs
Path: Freq.Table1.OneWayFreqs
-------------
Now you can use that with ods output:
ods output OneWayFreqs=myfreqdata;
proc freq data=sashelp.class;
tables age;
run;
ods output close; *this is technically not needed, but I like to have it for clarity;
It will make a table called myfreqdata (name that something useful please!). It isn't always "pretty"; sometimes you are better off using the built in output options in a proc, because they're more usefully formatted, but usually you can get somewhere from it.
Good afternoon,
It sounds like you want to output the results of the PROC FREQ to a SAS dataset. This will allow you to use them later, whether it is for further analysis or visualization. To do that, simply pass the a dataset name to the out= option after the table statement. You will notice below I also used the noprint option. This avoids needlessly printing the table.
For example,
proc freq data=work.dataset noprint;
table var1*var2 / out=work.output_data;
run;
I hope this helps!

SAS only include chi-square output in proc freq

I'm using the following code to do a chi-square test in SAS.
proc freq data=data;
tables var1*var2/chisq;
run;
It gives the output of both chi-square and Fisher's Test. Anyway to only include chi-square test in the output?
You can use ODS SELECT/EXCLUDE to control what is displayed. If you don't want the fishers test you can exclude that table, assuming the table name is FISHERSEXACT.
ods exclude fishersexact;
Alternatively you can select in only the tables you want and everything else is excluded by default.
ods select crosstabfreqs chisquare;
I do not think you can supress printing some of the tables in PROC FREQ; it seems to me you can supress all, or none.
However, still you can work as follows:
First, do a one-off investigation: issue the ods trace on / label before your statement so that the Log contains labels of the ODS tables:
ods trace on / label;
proc freq data=data;
tables var1*var2/chisq;
run;
ods trace off;
Alternatively, find the name of your table in PROC FREQ docs.
Once you know the table name, run your code again but this time save the respective ODS table to a regular table. This is done by issuing ods output <ODS=table-name>= <table-to-save-it-into>; in front of your command. Assuming you want to save the ChiSq table the code looks like:
ods output ChiSq=work.my_chisq_table;
proc freq data=data;
tables var1*var2/chisq;
run;
The chi-square table will be saved as work.my_chisq_table and you can print it elsewhere.

SAS RobustReg Procedure output parameter estimates [duplicate]

Running complex procs such as PROC REG or PROC GLM, there are often tables that are produced in the output window describing the results of the regression, in addition to the output datasets produced using OUT or OUTPUT options.
How can I output those tables to SAS datasets?
For example, given the first SAS example in PROC REG (on the documentation page), how can I output the Goodness of Fit Statistics (such as the R-Squared)?
In order to identify possible output datasets, SAS provides the ods trace statement. This asks SAS to write to the log the name (and some details) of each data table it writes to the output. In most cases, this can be saved to a dataset via ods output.
For example, in the SAS example referred to in the question, you could write:
ods trace on;
proc reg data=baseball;
id name team league;
model logSalary = no_hits no_runs no_rbi no_bb yr_major cr_hits;
run;
ods trace off;
That would report in the log that "FitStatistics" is the name of the output object you want. Then you write:
ods output FitStatistics=fitds;
proc reg data=baseball;
id name team league;
model logSalary = no_hits no_runs no_rbi no_bb yr_major cr_hits;
run;
and it will output the fitds dataset.
ODS Trace is only needed for the purpose of determining the name of the table of course - once you know the name of the table you need, you can simply use that name with ods output in the future.
You also frequently can find the list of the table names in the documentation; for example, PROC REG places them here.
ODS Output may be placed any location before the run statement (as it is a global statement); a common location is immediately before run. My personal preference is to put it before the proc as it is a global statement, but there is some disagreement with that approach.

How do I see what output options are available in my proc?

Running complex procs such as PROC REG or PROC GLM, there are often tables that are produced in the output window describing the results of the regression, in addition to the output datasets produced using OUT or OUTPUT options.
How can I output those tables to SAS datasets?
For example, given the first SAS example in PROC REG (on the documentation page), how can I output the Goodness of Fit Statistics (such as the R-Squared)?
In order to identify possible output datasets, SAS provides the ods trace statement. This asks SAS to write to the log the name (and some details) of each data table it writes to the output. In most cases, this can be saved to a dataset via ods output.
For example, in the SAS example referred to in the question, you could write:
ods trace on;
proc reg data=baseball;
id name team league;
model logSalary = no_hits no_runs no_rbi no_bb yr_major cr_hits;
run;
ods trace off;
That would report in the log that "FitStatistics" is the name of the output object you want. Then you write:
ods output FitStatistics=fitds;
proc reg data=baseball;
id name team league;
model logSalary = no_hits no_runs no_rbi no_bb yr_major cr_hits;
run;
and it will output the fitds dataset.
ODS Trace is only needed for the purpose of determining the name of the table of course - once you know the name of the table you need, you can simply use that name with ods output in the future.
You also frequently can find the list of the table names in the documentation; for example, PROC REG places them here.
ODS Output may be placed any location before the run statement (as it is a global statement); a common location is immediately before run. My personal preference is to put it before the proc as it is a global statement, but there is some disagreement with that approach.

Output to new data set

Suppose that I have a model and I want to output the studentized residuals, leverages, Cook's Distance and the DFFITS statistics from my regression model to a new data set. How would I do this?
Answering as a general question of how do I get certain pieces of output from a proc to a dataset, you will want to look at ODS TRACE.
ods trace on;
proc reg <stuff>;
<stuff>;
run;
ods trace off;
Now, look at the log, and see what different output options you have. All of the different things that go to the screen will be here, plus additional tables sometimes that don't go to any output window by default. Find the name for the tables you want data from, and then direct them to an ods output statement.
ods output <name>=<datasetname>;
proc reg <stuff>;
<stuff>;
run;
ods output close;
You can specify multiple names and multiple output datasets, assuming you want more than one thing.