Macros SAS宏:引号的奇怪行为
我正在学习SAS并编写以下宏:Macros SAS宏:引号的奇怪行为,macros,sas,comma,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( ) ) );
%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:
为什么你要在
字段1
和字段2
之间吃我的逗号(,
)?我想如果你把这个替换掉
%let Resty = %sysfunc(cats(&Resty, %str(,), &Wordy));
用这个
%Let RESTY=&resty %str(,) &wordy;
因为您正在尝试学习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"
如果您喜欢循环(谁不喜欢):
使用qsysfunc代替sysfunc你能说一下你想用这个宏做什么吗?是的,我正在尝试这样做:
“field\u 1”、“field\u 2”、“field\u 3”
。在这个宏之后,我可以在Where IN宏的IN子句中使用这个字符串。当我试图在带有in的宏中使用不带引号的字符串字段1、字段2、字段3
时,我得到错误这一行%doi=2%到&Count+1我认为这是罪魁祸首。尝试%doi=2%到%EVAL(&Count+1)代码>
%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;