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!
%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.
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;
Or also,
data have;
input name $;
datalines;
andy
jane
may
ali
;
run;
data _null_;
set have;
rc = dosubl(cat('%ForFun(',name,')'));
run;
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;
%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.
Hi Miller,
Do you have any recommendation (book, website, lecture, etc.) if I want to learn more about this kind of tech/skill?
Appreciate!
@Chung-Li I second your question. The statsman @PaigeMiller is dangerously good
Ask for ways to grab his statistical skills
@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.
The macros book (published by SAS):
SAS Macro Language Magic
It does cover CALL EXECUTE, but not DOSUBL.
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!
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.