Reputation: 223
I'm very new to coding macros in SAS, so I'm mostly looking for a push in the right direction.
I'm trying to create a big macro program, and within it I'm using CALL SYMPUTX
to create several global macro variables that I reference within the macro program. My code is as follows:
options symbolgen mlogic;
filename ALBB 'C:\[file path]\AL.csv';
libname mylibx 'C:\[file path]\ALBB';
%macro baseball(al=mylibx.al, nl=myliby.nl, var1=salary, class1=position, pd=percent_deviation);
data &al;
infile ALBB dlm=',' dsd firstobs=5 obs=381;
input team :$20. name :$20. salary :comma12. position :$20.;
run;
proc print data=&al (firstobs=1 obs=6);
format &var1 comma12.;
run;
proc means data=&al noprint;
var &var1;
class &class1;
output out=med_al median=med;
run;
data median_al;
set med_al;
if _TYPE_ = 0 then delete;
run;
****Macro Variables in question are below****
data _NULL_;
set &al;
if &class1='Catcher' then call symputx('MedianC',med);
if &class1='Catcher' then call symputx('FrequencyC',_FREQ_);
if &class1='First Baseman' then call symputx('MedianFB',med);
if &class1='First Baseman' then call symputx('FrequencyFB',_FREQ_);
if &class1='Outfielder' then call symputx('MedianO',med);
if &class1='Outfielder' then call symputx('FrequencyO',_FREQ_);
if &class1='Pitcher' then call symputx('MedianP',med);
if &class1='Pitcher' then call symputx('FrequencyP',_FREQ_);
if &class1='Second Baseman' then call symputx('MedianSB',med);
if &class1='Second Baseman' then call symputx('FrequencySB',_FREQ_);
if &class1='Shortstop' then call symputx('MedianS',med);
if &class1='Shortstop' then call symputx('FrequencyS',_FREQ_);
if &class1='Third Baseman' then call symputx('MedianTB',med);
if &class1='Third Baseman' then call symputx('FrequencyTB',_FREQ_);
run;
data temp;
set &al;
if &class1='Catcher' then &pd=((&var1-&MedianC)/(&MedianC))*100;
if &class1='First Baseman' then &pd=((&var1-&MedianFB)/(&MedianFB))*100;
if &class1='Outfielder' then &pd=((&var1-&MedianO)/(&MedianO))*100;
if &class1='Pitcher' then &pd=((&var1-&MedianP)/(&MedianP))*100;
if &class1='Second Baseman' then &pd=((&var1-&MedianSB)/(&MedianSB))*100;
if &class1='Shortstop' then &pd=((&var1-&MedianS)/(&MedianS))*100;
if &class1='Third Baseman' then &pd=((&var1-&MedianTB)/(&MedianTB))*100;
run;
proc print data=temp (firstobs=1 obs=10);
title 'Information about Baseball Players';
footnote1 %sysfunc(putn(&MedianC, dollar12.)) &FrequencyC;
footnote2 %sysfunc(putn(&MedianFB, dollar12.)) &FrequencyFB;
footnote3 %sysfunc(putn(&MedianO, dollar12.)) &FrequencyO;
footnote4 %sysfunc(putn(&MedianP, dollar12.)) &FrequencyP;
footnote5 %sysfunc(putn(&MedianSB, dollar12.)) &FrequencySB;
footnote6 %sysfunc(putn(&MedianS, dollar12.)) &FrequencyS;
footnote7 %sysfunc(putn(&MedianTB, dollar12.)) &FrequencyTB;
run;
%mend baseball;
%baseball()
While I'm not sure if I'm doing any of this correctly, it's definitely clear that my 'MedianX'
and 'FrequencyX'
macro variables aren't working. They resolve to missing values (whereas outside of the macro program, they resolve correctly).
Could anyone offer some guidance? Thanks very much!
Upvotes: 0
Views: 1054
Reputation: 81
Just copying this into SAS shows that the data null statement line 28 is removed due to the comment on line 27 having no ending semi colon. With that adding, this works for me based on the spread sheet I made.
Also, the null and temp tables have set &al which is your first dataset so does not contain the med and FREQ variables, these should read set median_al for this to resolve correctly.
Upvotes: 4