迭代SAS中的宏列表

时间:2017-10-24 13:48:54

标签: sas sas-macro

我第一次尝试使用宏变量,

我每个月都会生成很多表,我希望每个表只有两个变量,所以我创建了一个宏来避免代码中的冗余,但问题是宏变量在数据步骤中无法识别< / p>

    %LET OLDTABLES = 
oc201710.A20171001_active_households
oc201709.A20170901_active_households ;


* List of tables processed (ex with two tables);
%LET NEWTABLES = 
A20171001_HH
A20170901_HH ;


%MACRO VARTEST(x1=&OLDTABLES,x2=&NEWTABLES);



* Loop over the table;
%do i=1 %to %sysfunc(countw(&x1.));
    %LET FLAGP = %sysfunc(scan(&x2., &i));
    * get the 7 first characters from the table name;
    %LET VARFLAG = %sysfunc(substr(&FLAGP,1,7));
    %LET OLDTAB = %scan(&x1., &i);
    %LET NEWTAB = %scan(&x2., &i);

    %PUT &OLDTAB;
    %PUT &NEWTAB;

    * data step: keeping only the key + create new variable from the name of the table;
    rsubmit;
    data &NEWTAB (keep=cust_key &VARFLAG_f_act_n &VARFLAG_Households);
    set &OLDTAB;
    &VARFLAG._f_act_n = f_act_n;
    &VARFLAG._Households = 1;
    run;
    endrsubmit;
%end;
%mend VARTEST;

options mprint mfile;
filename mprint 'external-file';    
%VARTEST(x1=&OLDTABLES.,x2=&NEWTABLES.);

这是我作为输出得到的:

    836
837  * data step: keeping only the key + create new variable from the name of the table
838  options mprint mfile;
WARNING: Apparent symbolic reference OLDTABLES not resolved.
WARNING: Apparent symbolic reference NEWTABLES not resolved.
MPRINT(VARTEST):   * Loop over the table;
839  filename mprint 'external-file';
SYMBOLGEN:  Macro variable X1 resolves to &OLDTABLES.
WARNING: Apparent symbolic reference OLDTABLES not resolved.
840  %VARTEST(x1=&OLDTABLES.,x2=&NEWTABLES.);
SYMBOLGEN:  Macro variable X2 resolves to &NEWTABLES.
WARNING: Apparent symbolic reference NEWTABLES not resolved.
SYMBOLGEN:  Macro variable I resolves to 1
MPRINT(VARTEST):   * get the 7 first characters from the table name;
SYMBOLGEN:  Macro variable FLAGP resolves to NEWTABLES
SYMBOLGEN:  Macro variable X1 resolves to &OLDTABLES.
WARNING: Apparent symbolic reference OLDTABLES not resolved.
SYMBOLGEN:  Macro variable I resolves to 1
SYMBOLGEN:  Macro variable X2 resolves to &NEWTABLES.
WARNING: Apparent symbolic reference NEWTABLES not resolved.
SYMBOLGEN:  Macro variable I resolves to 1
SYMBOLGEN:  Macro variable OLDTAB resolves to OLDTABLES
OLDTABLES
SYMBOLGEN:  Macro variable NEWTAB resolves to NEWTABLES
NEWTABLES
MPRINT(VARTEST):   rsubmit

1 个答案:

答案 0 :(得分:2)

首先,您似乎没有运行定义&oldtables&newtables的代码部分。

其次,您的%SYSFUNC(COUNTW(...错了。 COUNTW将许多内容作为分隔符,包括.,这意味着您有超过2个单词。给它可选的第二个参数:

%do i=1 %to %sysfunc(countw(&x1.,%str( )));

(顺便说一下,你是如何用宏语言展示空间的。)

第三,你有一些问题,宏变量没有被正确分隔。 &VARFLAG_f_act_n需要&VARFLAG._f_act_n;你可以在以后做到这一点,但你不会在任何变量的keep语句中。 (顺便说一下,你可能只需keep &varflag:加上该列表中的其他变量。)

这对我有用。由于没有数据集,我显然无法做到所有可能的事情,但这可以按预期工作。

此外,我希望您了解您的RSUBMIT会话不了解宏变量。在RSUBMIT发生之前,它们会被本地会话解析。但是,如果您有一些需要在服务器端处理的宏变量,那么您必须%SYSLPUT%SYSRPUT该变量。

 %LET OLDTABLES = oc201710.A20171001_active_households oc201709.A20170901_active_households ;

* List of tables processed (ex with two tables);
%LET NEWTABLES = A20171001_HH A20170901_HH ;
options autosignon=yes;

%let sasloc = "C:\Program Files\sas94\sasfoundation\9.4\sas.exe";  *path to sas.exe;

signon process=a 
        sascmd=&sasloc.;


%MACRO VARTEST(x1=&OLDTABLES,x2=&NEWTABLES);



* Loop over the table;
%do i=1 %to %sysfunc(countw(&x1.,%str( )));
    %LET FLAGP = %sysfunc(scan(&x2., &i));
    * get the 7 first characters from the table name;
    %LET VARFLAG = %sysfunc(substr(&FLAGP,1,7));
    %LET OLDTAB = %scan(&x1., &i);
    %LET NEWTAB = %scan(&x2., &i);

    %PUT &OLDTAB;
    %PUT &NEWTAB;

    * data step: keeping only the key + create new variable from the name of the table;
    rsubmit process=a;
    %put &=newtab. &=oldtab. &=Varflag.;
    endrsubmit;
%end;
%mend VARTEST;

%VARTEST(x1=&OLDTABLES.,x2=&NEWTABLES.);