【问题标题】:SAS macros: Strange behavior of quotesSAS 宏:引号的奇怪行为
【发布时间】:2012-04-05 09:06:12
【问题描述】:

我正在学习 SAS 并编写此宏:

  %macro firstMacro(mvLO, OLO);
    %local Count;
    %local Wordy;
    %local Resty;
        %let Resty = '';
        %let Count = %sysfunc( count( &OLO, %str( ) ) );
        %let Wordy = %sysfunc( scan(&OLO, 1 ,%str( ) ) );
        %let Wordy = "&Wordy";
        %let Resty = &Wordy;
        %put &Resty;
        /*strange behavior here*/
    %DO I=2 %TO &Count+1;       
        %let Wordy = %sysfunc(scan(&OLO, &I ,%str( ) ));
        %let Wordy = "&Wordy";
        %put Wordy is;
        %put &Wordy;
        %let Resty = %sysfunc(cats(&Resty, %str(,), &Wordy));
        %put &Resty;
    %END;
        %put FINAL OUT;
        %put &Resty;        
  %mend firstMacro;

并调用它:

  %firstMacro(mvLO=WORK, OLO=field_1 field_2 field_3);

看看这个输出:

  FINAL OUT
  "field_1""field_2","field_3

所以,我问 SAS: 为什么你在field_1field_2之间吃我的逗号(,)?

【问题讨论】:

  • 你能说一下你想用这个宏做什么吗?
  • 是的,我正在尝试这样做:"field_1","field_2","field_3"。在这个宏之后,我可以在宏的 Where 中将此字符串用于 IN 子句。当我尝试在 WHERE with IN 的宏中使用不带引号 field_1,field_2,field_3 的字符串时,出现错误
  • 这条线%DO I=2 %TO &Count+1;是我认为的罪魁祸首。试试%DO I=2 %TO %EVAL(&Count+1);

标签: macros sas comma


【解决方案1】:

我想如果你替换这个

%let Resty = %sysfunc(cats(&Resty, %str(,), &Wordy));

有了这个

%Let RESTY=&resty %str(,) &wordy;

它会起作用(至少在您的示例调用中)

【讨论】:

    【解决方案2】:

    因为您正在尝试学习 SAS。这是一个更短的宏来做同样的事情。

    %macro firstMacro(mvLO, OLO);
        %local str1 str2 str3;
        %let str1=%sysfunc( strip(%sysfunc(compbl(&OLO))));
        %let str2=%sysfunc( transtrn(&str1,%str( ),%str(, ) )) ;
        %let str3=%sysfunc( catq(2csa, &str2));
        %put &str3;
     %mend firstMacro;
     %firstMacro(mvLO=WORK, OLO=field_1 field_2 field_3);
    

    在日志中

    "field_1","field_2","field_3"
    

    【讨论】:

      【解决方案3】:

      如果你喜欢循环(谁不喜欢):

      %macro firstMacro(mvLO=, OLO=);
      
         %* Note we dont need to make those macrovariables local in a macro - they;
         %* should be local to the macro unless you specifically make them global;
      
         %* Get a counter started ;
         %let i = 1;
      
         %* Initiate your new string to be empty;
         %let resty = ;
      
         %* Loop over your inputs until there are none left *;  
         %do %until(%scan(&OLO, &i) = );    
      
            %* Add the quotes and the comma;
            %let resty = &resty "%scan(&OLO, &i)", ;
      
            %* Update the counter;
            %let i = %eval(&i + 1);
         %end;
      
         %* Get rid of that trailing comma;
         %let resty = %substr(%nrbquote(&resty, 1, %eval(%length(&resty) - 1));
      
         %* Output to the log;
         %put &resty;
      %mend;
      

      【讨论】:

        【解决方案4】:

        使用 qsysfunc 代替 sysfunc

        【讨论】:

          猜你喜欢
          • 2018-02-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多