- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Using SAS 9.4.
I have the following code. Is there a way to edit the macro to enable a list of xvars instead of having to list each new xvar? I have 20+ variables and it would be nice to not have to write those out. Thank you
%macro getChiFish(data=, xvar=, grpvar=, outDat=);
proc freq data=&data noprint;
Tables &xvar * &grpvar/ missing nocum chisq fisher nopercent norow;
output out=tmpDat n nmiss pchi lrchi fisher ;
run;
%LET tmpvar = &xvar;
data &outDat;
set tmpDat (keep = P_PCHI XP2_FISH);
varN = symget('tmpvar');
run;
%mend getChiFish;
%getChiFish(data=para.analysis, xvar=sex, grpvar=recur, outDat=pval1)
%getChiFish(data=para.analysis, xvar=race, grpvar=recur, outDat=pval2)
%getChiFish(data=para.analysis, xvar=procedureid, grpvar=recur, outDat=pval3)
%getChiFish(data=para.analysis, xvar=proc_technique, grpvar=recur, outDat=pval4)
%getChiFish(data=para.analysis, xvar=smoke, grpvar=recur, outDat=pval5)
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I am going to say "GAAACK!".
There really is no reason to load the data set into Proc freq that many times. () will group variables together in proc freq.
proc freq data=&data noprint; Tables (xvarlist) * (grpvarlist)/ missing nocum chisq fisher nopercent norow; output out=tmpDat n nmiss pchi lrchi fisher ; run;
So if you use
ods output chisq=chi_squared fishersexact=fisher;
proc freq data=&data noprint; Tables (sex race procedureid proc_techique smoke) * (recur)/ missing nocum chisq fisher nopercent norow; run;
You get all the analysis in one pass. Use ODS OUTPUT with the desired tables as @PaigeMiller has shown and all of the results go into data sets with information to extract as needed such as table identification, variables used and the results.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
The general method of figuring out how to do this, for almost every PROC, is to use ODS TRACE. Example:
ods trace on;
proc freq data=sashelp.class;
tables sex*age/chisq exact;
run;
ods trace off;
In the log you will see:
Output Added: ------------- Name: CrossTabFreqs Label: Cross-Tabular Freq Table Template: Base.Freq.CrossTabFreqs Path: Freq.Table1.CrossTabFreqs ------------- Output Added: ------------- Name: ChiSq Label: Chi-Square Tests Template: Base.Freq.ChiSq Path: Freq.Table1.ChiSq ------------- Output Added: ------------- Name: FishersExact Label: Fisher's Exact Test Template: Base.Freq.ChisqExactFactoid Path: Freq.Table1.FishersExact -------------
so these are the names of the tables in SAS you want to output to a data set. Use ODS OUTPUT like this, where to the left of the equal signs are the table names shown above, and to the right of the equal signs are the name of the SAS data set you want to create.
ods output chisq=chi_squared fishersexact=fisher;
proc freq data=sashelp.class;
tables sex*age/chisq exact;
run;
Paige Miller
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I am going to say "GAAACK!".
There really is no reason to load the data set into Proc freq that many times. () will group variables together in proc freq.
proc freq data=&data noprint; Tables (xvarlist) * (grpvarlist)/ missing nocum chisq fisher nopercent norow; output out=tmpDat n nmiss pchi lrchi fisher ; run;
So if you use
ods output chisq=chi_squared fishersexact=fisher;
proc freq data=&data noprint; Tables (sex race procedureid proc_techique smoke) * (recur)/ missing nocum chisq fisher nopercent norow; run;
You get all the analysis in one pass. Use ODS OUTPUT with the desired tables as @PaigeMiller has shown and all of the results go into data sets with information to extract as needed such as table identification, variables used and the results.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Adding to @ballardw 's comment, you might want the NOPRINT option in the TABLES statement as well (or maybe you don't want the NOPRINT option, its up to you, but typically when you are outputting to data sets, you don't also need the same output in the Results window).
Paige Miller
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
ODS output requires the result output. Otherwise you're restricted to the old OUTPUT statement limit of only the last table generated would create a dataset. Which would lead us to multiple TABLE statements and separate output data sets for each.