SAS宏:引号的奇怪行为

时间:2012-04-05 09:06:12

标签: macros sas comma

我正在学习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_1之间吃逗号(field_2)?

4 个答案:

答案 0 :(得分:1)

我想如果你替换这个

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

用这个

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

它会起作用(至少是你的样本电话)

答案 1 :(得分: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"

答案 2 :(得分: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;

答案 3 :(得分:0)

使用qsysfunc代替sysfunc