BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Chung-Li
Quartz | Level 8

 

Hi all,

 

 

I'm trying to do the same procedure for a set of known variables, say proc freq for {andy, jane, may, ali}. There are two ways I can think of:

1. brute force

 

PROC FREQ;
TABLE andy;
TABLE jane;
TABLE may;
TABLE ali;
RUN;

2. brute force(macro version)

 

%MACRO ForFun(Variable);
PROC FREQ;
TABLE &Variable;
RUN;
%MEND;
%ForFun(andy);
%ForFun(jane);
%ForFun(may);
%ForFun(ali);

Sort of want to hear any better solution about this, thanks guys!

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
%let variables=andy jane may ali;
PROC FREQ;
    TABLE &Variables;
RUN;

The above works, but of course it would be even less typing than if you didn't use the macro variable at all and used. 

 

TABLE andy jane may ali;

 But perhaps you aren't typing the variable names, you are obtaining them from PROC CONTENTS or other source... in which case using a macro variable would work better.

--
Paige Miller

View solution in original post

11 REPLIES 11
ed_sas_member
Meteorite | Level 14

Hi @Chung-Li 

 

DOSUBL (= CALL EXECUTE) can be a good alternative:

data have;
	input name $;
	datalines;
andy
jane
may
ali
;
run;

data _null_;
	set have;
	rc = dosubl(cat('proc freq data=mydata;table ',name,'; run;')); /*specify the name of your dataset*/
run;

 

ed_sas_member
Meteorite | Level 14

Or also,

data have;
	input name $;
	datalines;
andy
jane
may
ali
;
run;

data _null_;
	set have;
	rc = dosubl(cat('%ForFun(',name,')'));
run;

 

 

Chung-Li
Quartz | Level 8
Thanks ed, skill learned!
Kurt_Bremser
Super User
data vars;
input varname $;
datalines;
andy
jane
may
ali
;

data _null_;
set vars end=done;
if _n_ = 1 then call execute('proc freq;');
call execute('table ' !! varname !! ';');
if done then call execute('run;');
run;
Chung-Li
Quartz | Level 8
Thanks Kurt!
PaigeMiller
Diamond | Level 26
%let variables=andy jane may ali;
PROC FREQ;
    TABLE &Variables;
RUN;

The above works, but of course it would be even less typing than if you didn't use the macro variable at all and used. 

 

TABLE andy jane may ali;

 But perhaps you aren't typing the variable names, you are obtaining them from PROC CONTENTS or other source... in which case using a macro variable would work better.

--
Paige Miller
Chung-Li
Quartz | Level 8

Hi Miller,

 

Do you have any recommendation (book, website, lecture, etc.) if I want to learn more about this kind of tech/skill?

Appreciate!

 

novinosrin
Tourmaline | Level 20

@Chung-Li   I second your question. The statsman @PaigeMiller  is dangerously good 

 

Ask for ways to grab his statistical skills 

PaigeMiller
Diamond | Level 26

@Chung-Li wrote:

Hi Miller,

 

Do you have any recommendation (book, website, lecture, etc.) if I want to learn more about this kind of tech/skill?

Appreciate!

 


So "this kind of tech/skill" is somewhat vague ... I don't really know if you want to learn more about programming and statistical analysis, or learn more about macros/CALL EXECUTE/DOSUBL, or all of the above.

 

I do like the book by Mr. @Astounding, but as I no longer own the book (my employer bought it for me and kept it when I moved to a new employer), I don't remember the name of the book, but I'm sure Mr. @Astounding will inform us. I'm also not sure if this book covers what you are discussing.

--
Paige Miller
Chung-Li
Quartz | Level 8
Sorry for the unclarity.

I didn't know which term/area should I check for "%let", but now I totally know where to check, thanks!
Astounding
PROC Star

The macros book (published by SAS):  

 

SAS Macro Language Magic

 

It does cover CALL EXECUTE, but not DOSUBL.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 11 replies
  • 1275 views
  • 4 likes
  • 6 in conversation