BookmarkSubscribeRSS Feed
leeleelee
Calcite | Level 5

Hi, I currently have a proc freq macro.
How do I separate these into separate proc freq's for each variable?

 

Note: I have 13 categorical variables and 2 numerical variables.

/*looping through all categorical variables for proc freq exports*/
%macro freq_loop;
%Do i=1 %to &num_cats.;
	%let j=%scan(&cat_vars.,&i.);

proc freq data=&filetype._stacked order=freq; 
	table &j. / out=freqs_&j. ;
	by year;
run;

PROC EXPORT DATA=freqs_&j.
	OUTFILE="&results.\&filetype._ProcFreq_&date..csv"
	dbms=xlsx replace;
	sheet= "&j.";
RUN;

%end;
%mend;
%freq_loop;

/*looping through all numerical variables for proc means exports*/
%macro means_loop;
%Do i=1 %to &num_nums.;
	%let j=%scan(&num_vars.,&i.);

proc means data=&filetype._stacked; 
	var &j.;
	by year;
	output out=means_&j. (where=(_STAT_ IN ("MIN", "MEAN", "MAX"))drop=_TYPE_ _FREQ_);
run;

PROC EXPORT DATA=means_&j.
	OUTFILE="&results.\&filetype._ProcMeans_&date..csv"
	dbms=xlsx replace;
	sheet= "&j.";
RUN;
%end;
%mend;
%means_loop;

Thank you in advance,

4 REPLIES 4
Reeza
Super User
That's already what it's doing - it does it by year though. Are you asking how to do it not by year as well?

FYI - CSVs do not support multiple sheets, its a single file so you cannot export with a CSV extension, specify the DBMX=XLSX and a sheet name. Also, have you considered using ODS EXCEL instead of EXPORTS?

leeleelee
Calcite | Level 5

Hello,

I am wondering how to take it out of the macro (a.k.a. the looping), group by year (I have a total of 12 years)
I want to write it out for example (I know this code is wrong):

proc freq cat_var ...

and thank you for the csv tip. I'll note it.

Reeza
Super User

Something like this:

proc freq data=inputDatasetName;
table SingleVariable orListOfVarsSpaceDelimited;
run;

proc freq data=sashelp.class;
table sex age name;
table sex;
table age;
table sex*age;
run;

Maybe this tutorial would be helpful:

https://libguides.library.kent.edu/SAS/Frequencies

leeleelee
Calcite | Level 5

I'll try this out and will see if it works!
Thank you!

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1124 views
  • 0 likes
  • 2 in conversation