全局宏变量(使用 CALL SYMPUTX 创建)在 SAS 宏程序中无法正确解析

Global macro variables (created with CALL SYMPUTX) won't resolve properly within SAS macro program

我对在 SAS 中编写宏代码还很陌生,所以我主要是在寻找正确方向的推动力。

我正在尝试创建一个大型宏程序,我在其中使用 CALL SYMPUTX 创建几个我在宏程序中引用的全局宏变量。我的代码如下:

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 :. name :. salary :comma12. position :.;
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()

虽然我不确定我是否正确地执行了这些操作,但很明显我的 'MedianX''FrequencyX' 宏变量没有工作。它们解析为缺失值(而在宏程序之外,它们解析正确)。

谁能提供一些指导?非常感谢!

只需将其复制到 SAS 中,就会发现由于第 27 行的注释没有结束分号,因此删除了数据空语句第 28 行。有了这个添加,这对我有用 sheet 我做的价差。

此外,null 和临时表已设置 &al,这是您的第一个数据集,因此不包含 med 和 FREQ 变量,这些应该阅读 set median_al 才能正确解析。