%macro name_modal();
/*Create macro variables which contain the modalities of variables*/
%do i=1 %to &num_Var;
data _null_;
set &lib..Table_variable_modal_&i.;
call symputx('num_Mod'||compress(put(&i,4.)),_N_,"G");
call symputx('table'||compress(put(&i,4.))||'modal'||compress(put(_N_,4.)),compress(&&name_Var&i.),"G");
run;
%end;
/*Display modalities by variable*/
%do i=1 %to &num_Var;
%put &&name_Var&i. has &&num_Mod&i. modalities ;
%do j=1 %to &&num_Mod&i.;
%put %nrstr(&&tableb&i.modal&j.);
%end;
%end;
%mend name_modal;
%name_modal();
我希望代码是自记录的。
我将在这里解释问题。
一切正常,直到我转到程序的第二个,该程序用于通过变量显示模式。
例如,当宏变量中存储的模态名称类似于
100% BLO,
100% 颜色,
雅芳和拉戈伯特,
百龄坛,
欧莱雅,
美国电话电报公司,
U-V-A
等等
我没有正确使用 %put。
我试过使用 %bquote 和 %nrstr,但问题仍然存在。
到目前为止,我能看到的唯一解决方案是修改模式的名称,但由于名称来自客户,我无法对数据进行修改。
谢谢
最佳答案
尝试了几次后,我发现 %superq
可以解决这个问题。在宏中处理特殊字符很麻烦。这个 page 提供了一些关于宏引用的有用提示。
我将您的代码简化为以下内容
更新:使其成为双循环案例。
data test;
input name ~ & $15.;
datalines;
100% BLO
100% COLOR
AVON & RAGOBERT
BALLANTINE'S
L'OREAL
AT&T
U-V-A
;
run;
%macro name_modal();
/*Create macro variables which contain the modalities of variables*/
%do i=1 %to 4;
data _null_;
set test;
call symputx('num_Mod1',_N_,"G");
call symputx('tableb'||compress(put(&i,4.))||'modal'||compress(put(_N_,4.)),name,"G");
run;
%end;
%do i=1 %to 4;
%do j=1 %to 7;
%put %superq(tableb&i.modal&j);
%end;
%end;
%mend name_modal;
%name_modal();
结果将正确显示。
请注意,它是
%superq(tableb&i.modal&j)
而不是 %superq(&&tableb&i.modal&j)
,因为 superq
接受没有额外符号的宏变量名称。