1

我正在学习 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

4

4 回答 4

1

我想如果你更换这个

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

有了这个

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

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

于 2012-04-05T12:32:42.863 回答
1

由于您正在尝试学习 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"
于 2012-04-05T17:30:23.083 回答
0

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

%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;
于 2012-04-06T00:18:22.803 回答
0

使用 qsysfunc 代替 sysfunc

于 2014-03-11T10:09:09.340 回答