BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
GS2
Obsidian | Level 7 GS2
Obsidian | Level 7

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)
1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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.

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26

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
ballardw
Super User

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.

PaigeMiller
Diamond | Level 26

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
ballardw
Super User

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.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 1756 views
  • 1 like
  • 3 in conversation