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 に答える