SAS在Unix服务器上定义宏

时间:2018-08-28 15:00:14

标签: unix server macros sas

嗨,我正在尝试在Unix服务器上定义和运行SAS宏。

options symbolgen mprint mlogic;
*options nosymbolgen nomprint nomlogic;

rsubmit;

%let trend_MM = 1;
%let run_date = &sysdate.;

/* loop through Trend_MM number of times to retrieve the MTR completed per each month. */
%macro Trend();
%local start_dt;
%local end_dt;
%local i;

/* loop through each month */
%do i = &Trend_MM. %to 0 %by -1;

/* Calculate set start/end dates to start/end of month */
%let start_dt = %qsysfunc(intnx(month,"&run_date."d,%eval(-1*&i.),b),date9.);
%let end_dt = %qsysfunc(intnx(month,"&run_date."d,%eval(-1*&i).,e),date9.);

    /* select MTR claims in month */
    proc sql;
        create table MTR_&i. as
        select a.claim_id_360
              ,a.claim_id_external 
              ,a.timestamp
              /* v02: a) export date part of the timestamp */
              ,datepart(a.timestamp) as timestamp_dt
              ,a.status_data 
              ,a.status_label 
              ,a.RFE_template_name 
              ,a.brand_name
              ,case when b.claim_id_360 is not missing then "Y" else "N" end as MTR_compl
              ,c.MTR_count
              ,b.last_MTR_comp format = ddmmyy10.
        from 
            /* Base population are all claims where final status is not   */
            (
             select distinct *
             from allg360r._travel_rfe_state_change_hist
             (keep = claim_id_360 claim_id_external timestamp status_data status_label RFE_template_name brand_name)
             where datepart(timestamp) between "&start_dt."d and "&end_dt."d
             group by claim_id_360 
             having timestamp = max(timestamp) and strip(upcase(status_label)) ne "Move to Review"
            ) a

            left join 
        /*     subset with claims that have had a  status in the reference period to obtain  */
        /*     claims that have had such status completed in the period.                                     */
        /*     Also retrieve date of last MTR completed                                                      */
            (
             select distinct claim_id_360, datepart(timestamp) as last_MTR_comp
             from allg360r._travel_rfe_state_change_hist
             (keep = claim_id_360 claim_id_external timestamp status_data status_label RFE_template_name brand_name)
             where (datepart(timestamp) between "&start_dt."d and "&end_dt."d) and status_label = "Move to Review"
             group by claim_id_360
             having timestamp = max(timestamp)
            ) b
            on a.claim_id_360 = b.claim_id_360

            left join 

        /*   Add count of how many since inception of claim  */
        /* v02: b) count how many times in MTR since beginning */
            (
             select distinct
                    claim_id_360
                   ,count(claim_id_360) as MTR_count
             from allg360r._travel_rfe_state_change_hist
             (keep = claim_id_360 claim_id_external timestamp status_data status_label RFE_template_name brand_name)
             where status_label = "Move to Review"
             group by claim_id_360
            ) C
            on a.claim_id_360 = c.claim_id_360
        ;
        quit;

    /* create/append to dataset of all months */

    %if &i. = &trend_MM. %then 
      %do;
        proc sql;
            drop table work.MTR;
            create table work.MTR like work.MTR_&i.;
        quit;
      %end;

    proc append base = work.MTR data = work.MTR_&i.;
    run;
%end;

%mend Trend;

%trend

endrsubmit;

似乎工作正常,但我在日志中不断收到此错误:

  

995 endrsubmit;        ----------        180

     

错误180-322:该语句无效或使用不当   订单。

如果在宏调用后加上分号,该错误似乎消失了。这对我来说没有意义,因为宏以运行结束。并在调用后加上分号不会有任何区别。

有任何想法为什么会这样?

2 个答案:

答案 0 :(得分:2)

原因是-您用括号()

定义了宏

在调用中添加一些方括号,它将在不使用分号的情况下执行。

%trend()

答案 1 :(得分:2)

信不信由你,因为您使用空参数列表定义了宏:

%macro Trend();
  %put TREND running;
%mend trend;

当您尝试调用%trend之类的宏时,SAS希望您向其发送一个参数列表,即使是空列表也足够。因此,宏调用尚未结束。分号将结束宏调用,但是您没必要,它是正确的,添加额外的分号是一个坏习惯。当您调用已定义参数列表的宏(即使在这种情况下,即使在宏定义上括号也为0,因此在某种意义上仍然定义了该参数),都可以像%trend()那样调用它。括号结束了宏调用。

使用PC SAS,您只需突出显示%trend并提交即可很好地进行测试。宏将不会运行。然后,如果您提交(),则宏将运行。

更新: 由于无法纠正宏调用,因此我怀疑这可能是单词扫描程序如何识别endrsubmit语句的错误。假设它是可复制的,我可能会添加一条提供分号的注释。至少以这种方式,很明显分号不是结束宏调用的一部分。抱歉,我无法测试,因为没有SAS /连接器可用。

类似的东西:

* endrsubmit needs a semicolon before it ??? ;
endrsubmit ;