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

I need to run a proc freq for every code in a long list. Is there a macro that can be used to do this? 

For example, I need the following proc freq to run over and over again, each time replacing FieldName1.

 

Need to run proc freq for 'XYZ', 'ABC', 'R2D2', ..., etc. 

 

proc freq data=table;
where FieldName1= 'XYZ';
tables FieldName1*FieldName2 / norow nocol nopercent missing;
run;

 

proc freq data=table;
where FieldName1= 'ABC';
tables FieldName1*FieldName2 / norow nocol nopercent missing;
run;

 

proc freq data=table;
where FieldName1= 'R2D2';
tables FieldName1*FieldName2 / norow nocol nopercent missing;
run;

 

And so and forth for a long list of these codes.

1 ACCEPTED SOLUTION

Accepted Solutions
10 REPLIES 10
ChickenLittle
Obsidian | Level 7
I have a long list of codes I need to run a proc freq on. So my example, I need a proc freq run on 'XYZ' and then another proc freq run on 'ABC' and then another proc freq run on 'r2d2' and so on and so forth.
PaigeMiller
Diamond | Level 26

@ChickenLittle wrote:
I have a long list of codes I need to run a proc freq on. So my example, I need a proc freq run on 'XYZ' and then another proc freq run on 'ABC' and then another proc freq run on 'r2d2' and so on and so forth.

But this doesn't sound like a macro, this sounds like a BY statement.

--
Paige Miller
ChickenLittle
Obsidian | Level 7
Hi, Thank you for your response. I don't think it's a by statement. I have updated my original post to be clearer. Thanks.
PaigeMiller
Diamond | Level 26

So, @ChickenLittle, there are two lessons here, both of which I have tried to say

 

1. Don't tell us you need a macro. Tell us what you are trying to do (the big picture, the purpose of the analysis)

2. If you think you need a macro, show us code that does what you want without macros and without macro variables.

--
Paige Miller
Kurt_Bremser
Super User
%let values = "ABC","DEF","R2D2";

proc sort data=table;
by fieldname1;
run;

proc freq data=table (where=(fieldname1 in (&values.))),
by fieldname1;
tables fieldname2;
run;
ChickenLittle
Obsidian | Level 7
Thanks so much!!
PaigeMiller
Diamond | Level 26

@ChickenLittle wrote:

Is there a macro that can be used on proc freq to generate a frequency table for each variable in a list (it is a long list of codes)? For example, I was thinking of something like this, but his does not work.


%let Values = 'XYZ','ABC','R2D2' ;


proc freq data=table;
where FieldName1= %Values. ;
tables FieldName1*FieldName2
/ norow nocol nopercent missing;
run;


Your description in words, and your code, do not match. You say "to generate a frequency table for each variable in a list" but then you generate a two-way frequency table of FieldName1*FieldName2 and the list isn't used to create frequency tables. Which is it?

 

You then put the list into a WHERE statement. Would a BY statement work here where you generate FieldName1*FieldName2 for each value in the list?

 

This will never work:

 

where FieldName1= %Values. ;

 

and also demonstrates a poor approach to writing macros. A much better approach: produce WORKING code without macro and without macro variables first, before you try to create a working macro. Why? Because if your code without macros and without macro variables does not work, then the code for macros will never work either.

 

So, please please please, show us WORKING code without macros and without macro variables for one of the many situations that you envision this will need to work on. Showing us code that works also will clear up the confusion I felt when your original question wording did not match the code.

 

Lastly, please describe the big picture, the real problem here. What and why? (The real problem is not that you want a macro to work with PROC FREQ, the real problem is that you want to achieve some goal and produce some analysis, that's what I am asking for) Often macros are not needed, so don't start with saying you need a macro.

--
Paige Miller
Quentin
Super User

Agree with @PaigeMiller , this sounds like a BY-statement. You didn't post sample data, but consider a BY statement like:

 

proc freq data=sashelp.cars ;
  tables type*drivetrain/norow nocol nopercent missing ;
  by make ;
run ;

So with your pseudocode, try running:

proc freq data=table;
  tables FieldName1*FieldName2 / norow nocol nopercent missing;
  by FieldName1;
run;

and if that doesn't give you the output you want, please describe how the output you want differs.

BASUG is hosting free webinars Next up: Don Henderson presenting on using hash functions (not hash tables!) to segment data on June 12. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
ChickenLittle
Obsidian | Level 7
Thank you for taking out the time to provide examples. You were right, it was a by statement, but I needed to feed the specific FieldNames1 I wanted. I've got the answer now from another post. Thanks so much!!

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
  • 10 replies
  • 1234 views
  • 4 likes
  • 4 in conversation