DATA Step, Macro, Functions and more

Loop of variable in proc freq

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 9
Accepted Solution

Loop of variable in proc freq

Hi,

 

New here, I hope that I'm am posting my question in the good location.

 

In my previous work experience, I used STATA and Eviews. Now, in my new job, I must use SAS and therefore I am currently learning it. However I would have a question about loops.

 

I want to run a proc freq for many variables.

Here what the proc freq looks like for one variable:

proc freq data=ALL;

          table COMPLIANCE_PC1 / binomial(level=1) alpha=.05;

run;

 

I have around 15 more variables than COMPLIANCE_PC1 and I would like to learn how to loop the variable instead of writing the proq freq for each variable individually.

 

I thought that I could do the following lines, but it doesn't work.

%macro loop(var);

proc freq data=ALL;

          table &var / binomial(level=1) alpha=.05;

run;

%mend loop;

%loop(COMPLIANCE_PC1);

%loop(COMPLIANCE_PC2a);

...

%loop(COMPLIANCE_OTH2);

 

Thank you in advance,


Accepted Solutions
Solution
‎04-26-2017 01:25 PM
Super User
Posts: 5,516

Re: Loop of variable in proc freq

PROC FREQ allows you to add multiple TABLE statements.  You only need a single PROC FREQ.

 

In addition, a TABLE statement lets you include many table requests (just supply a list of variable names separated by spaces).  However, the options after the slash would likely apply to just one of the tables, so  you might need to revert to Plan A and supply many TABLE statements.

 

I would suggest avoiding macro language for now.  All it will do for you is construct a program (it won't process your data).  It's more important to spend your time learning what the program can or should look like.  Once you have more possible programming techniques available, it would become appropriate to consider macro language.  In this case, generating 15 PROC FREQs (rather than a single PROC FREQ with multiple TABLE statements) would take much longer to run.

View solution in original post


All Replies
Frequent Contributor
Posts: 93

Re: Loop of variable in proc freq

A possible solution for you:

 

PROC SQL;
CREATE TABLE COLNAMES AS SELECT NAME FROM DICTIONARY.COLUMNS WHERE MEMNAME = 'ALL';
QUIT;

/*CREATE A MACRO THAT WE WILL USE TO UPDATE NEWNAME WITH THE SPECIFIED CRITERIA*/
%MACRO LOOP(COLNAME);
proc freq data=ALL;
          table &COLNAME / binomial(level=1) alpha=.05;
run;
%MEND;

/*EXECUTE THE MACRO FOR EACH OF THE OLD NAMES*/
DATA COLNAMES;
SET COLNAMES;
CALL EXECUTE("%LOOP("||TRIM(NAME)||")");
RUN;
Solution
‎04-26-2017 01:25 PM
Super User
Posts: 5,516

Re: Loop of variable in proc freq

PROC FREQ allows you to add multiple TABLE statements.  You only need a single PROC FREQ.

 

In addition, a TABLE statement lets you include many table requests (just supply a list of variable names separated by spaces).  However, the options after the slash would likely apply to just one of the tables, so  you might need to revert to Plan A and supply many TABLE statements.

 

I would suggest avoiding macro language for now.  All it will do for you is construct a program (it won't process your data).  It's more important to spend your time learning what the program can or should look like.  Once you have more possible programming techniques available, it would become appropriate to consider macro language.  In this case, generating 15 PROC FREQs (rather than a single PROC FREQ with multiple TABLE statements) would take much longer to run.

Occasional Contributor
Posts: 9

Re: Loop of variable in proc freq

Posted in reply to Astounding

Thank you to you two!

Super User
Posts: 11,343

Re: Loop of variable in proc freq

And for added fun the tables statement will allow some moderately complex requests.

 

Tables (a b c) *( s t u v) ; would create a crosstab of each variable listed inside the first set of () with each in the second:

a*s a*t a*u a*v b*s b*t etc. And you can multiples of those.

 

Note also that

Proc freq data=somedatasetname;

run;

 

Will run frequencies on every single variable in the data set though only the basic counts and percent.

 

Also you want to look into variable lists. There are several ways to refer to groups of variables

 

Tables bac: / <options>; would generate output for each variable whose name starts with the character sequence bac.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 246 views
  • 3 likes
  • 4 in conversation