迭代列表时创建后缀

时间:2014-11-24 17:38:21

标签: sas

我很难尝试迭代宏%do循环中的值列表。每个值都应该用作变量后缀。

该方法基于SAS文档:http://support.sas.com/kb/26/155.html

(简化)代码是:

%macro loop(values);                        
     %let count=%sysfunc(countw(&values));
     %do i = 1 %to &count;          
      %let value=%qscan(values,i,%str(,));
        proc sql;
            select count(distinct hut_id) as prefix_&value.
            from saslib.tl1_results_eval
            group by plan_cell;
        quit;
     %end;                                                                             
%mend;                   
%loop(%str(a,b,c,d))

生成的错误消息为:

MLOGIC(LOOP):  %DO loop beginning; index variable I; start value is 1; stop value is 4; by value
      is 1.
MLOGIC(LOOP):  %LET (variable name is VALUE)
MPRINT(LOOP):   proc sql;
22: LINE and COLUMN cannot be determined.
NOTE 242-205: NOSPOOL is on. Rerunning with OPTION SPOOL might allow recovery of the LINE and
              COLUMN where the error has occurred.
ERROR 22-322: Syntax error, expecting one of the following: ',', AS.
MPRINT(LOOP):   select count(distinct hut_id) as prefix_a from group by plan_cell;
MPRINT(LOOP):   quit;

有趣的是,如果我删除"前缀_"从" count(distinct hut_id)作为prefix_& value。" - 它的工作非常好。不幸的是,我确实需要前缀。另外,正如您在日志消息中看到的那样,PROC SQL语句最终会被正确编译,但代码在执行之前会出错。

有什么想法在这里发生了什么?谢谢!

1 个答案:

答案 0 :(得分:1)

由于qscan功能,您正在插入不可见的引号字符。尝试:

  %macro loop(values);                   
    proc sql;     
    %let count=%sysfunc(countw(&values));
    %do i = 1 %to &count;          
      %let value=%qscan(values,i,%str(,));
      select count(distinct hut_id) as prefix_%unquote(&value)
        from saslib.tl1_results_eval
        group by plan_cell;
    %end;  
    quit;                                                               
  %mend;                   
  %loop(%str(a,b,c,d))

请注意proc sql;声明的移动 - 几乎没有充分的理由退出;并在每个SQL语句之间重新实例化SQL过程,这会产生开销..

相关问题