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

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,

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

4 REPLIES 4
thomp7050
Pyrite | Level 9

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;
Astounding
PROC Star

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.

Shawn08
Obsidian | Level 7

Thank you to you two!

ballardw
Super User

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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 5281 views
  • 3 likes
  • 4 in conversation