运行宏时没有获得所需的输出

时间:2017-01-12 10:20:17

标签: sas sas-macro

我有以下sas宏。我想创建一个数据集,我希望每年增加一些权重

对于宏变量chk_var中的每个变量。但它没有给出任何结果。在日志中它显示

MLOGIC(WEIGHT_AVG): %IF condition year=2015 is FALSE

MLOGIC(WEIGHT_AVG): %IF condition year=2014 is FALSE

MLOGIC(WEIGHT_AVG): %IF condition year=2013 is FALSE

MLOGIC(WEIGHT_AVG): %IF condition year=2012 is FALSE

MLOGIC(WEIGHT_AVG): %IF condition year=2011 is FALSE

MLOGIC(WEIGHT_AVG): %IF condition year=2010 is FALSE

任何帮助将不胜感激。

我的宏 -

%let chk_var=Total_Shareholders_Funds Secured_Loans Total_Debt___Loan_Funds
    Total_Liabilities Sundry_Debtors Inventories Cash_and_Bank_Balance
    Total_Current_Assets Total_Current_Liabilities Sls_Turnover_Operat_Incom
    Net_Sales Total_Income Operating_Profit Interest Gross_Profit  Profit_Before_Tax Tax 
    Reported_Net_Profit Adjusted_Net_Profit Debt_Equity_Ratio Interest_Cover_Ratio
ROCE RONW;


%put %sysfunc(countw(&chk_var.));


options mprint mlogic symbolgen;

%macro weight_avg;

Data capital_all_3;

set capital_all_2;

%do i=1 %to %sysfunc(countw(&chk_var.));

%let a=%scan(&chk_var.,&i.);

%if year=2015 %then &a._15=6*&a.;

%if year=2014 %then &a._14=5*&a.;

%if year=2013 %then &a._13=4*&a.;

%if year=2012 %then &a._12=3*&a.;

%if year=2011 %then &a._11=2*&a.;

%if year=2010 %then &a._10=1*&a.;

%end;

run;

%mend weight_avg;

%weight_avg;

1 个答案:

答案 0 :(得分:2)

您的%IF条件是比较文字字符串' year'到文字字符串' 2010' (等),这将永远是假的。宏%IF语句只比较文字字符串,但通常至少文字字符串来自正在解析的宏变量,如下所示:

%IF &year=2015 %THEN ...

但是,您的宏不包含名为YEAR的宏变量,因此我怀疑您是否尝试从数据集中引用变量。宏变量和数据集变量是两个完全不同的东西,所以这不会起作用。

尝试将其更改为数据步骤IF:

if year=2010 then &a._15=6*&a.;

宏程序是宏代码和普通基础SAS代码的混合,明确哪个是非常重要的!