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!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

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
  • 409 views
  • 0 likes
  • 2 in conversation