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,
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.
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;
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.
Thank you to you two!
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.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.