我试图通过为现有变量分配格式来创建新变量。我是在一个宏内做这个。我收到以下错误:“:期望格式名称。”有关如何解决的任何想法?谢谢!
/* macro to loop thru a list of vars and execute a code block on each. This is working fine. */
%macro iterlist
(
code =
,list =
)
;
%*** ASSIGN EACH ITEM IN THE LIST TO AN INDEXED MACRO VARIABLE &&ITEM&I ;
%let i = 1;
%do %while (%cmpres(%scan(&list., &i.)) ne );
%let item&i. = %cmpres(%scan(&list., &i.));
%let i = %eval((&i. + 1);
%end;
%*** STORE THE COUNT OF THE NUMBER OF ITEMS IN A MACRO VARIABLE: &CNTITEM;
%let cntitem = %eval((&i. - 1);
%*** EXPRESS CODE, REPLACING TOKENS WITH ELEMENTS OF THE LIST, IN SEQUENCE;
%do i = 1 %to &cntitem.;
%let codeprp = %qsysfunc(tranwrd(&code.,?,%nrstr(&&item&i..)));
%unquote(&codeprp.)
%end;
%mend iterlist;
/* set the list of variables to iterate thru */
%let mylist = v1 v2 v3 v4;
/* create a contents table to look up format info to assign in macro below*/
proc contents data=a.recode1 noprint out=contents;
run;
/* macro to create freq and chisq tables for each var */
%macro runfreqs (variabl = );
proc freq data=a.recode1 noprint ;
tables &variabl.*improved /out=&variabl._1 chisq;
output out=&variabl.chisq n pchi ;
run;
/* do some more stuff with the freq tables, then grab format for variable from contents */
data _null_;
set contents;
if name="&variabl." then CALL SYMPUT("classformat", format);
run;
data &variabl._3;
length classvalue $ 30 ;
set &variabl._2; ;
/* output a new var using the macro variable for format that we pulled from contents above. Here's where the error occurs. */
classvalue=put(class, %quote(&classformat.));
run;
%mend runfreqs;
* run the macro, iterating thru var list and creating freq tables;
%ITERLIST(list = &mylist., code = %nrstr(%runfreqs(variabl = ?);));
答案 0 :(得分:1)
猜猜,行
classvalue=put(class, %quote(&classformat.));
应该是
classvalue=put(class, &classformat..);
两点因为一个被宏处理器“吃掉”以标记宏变量名称的结尾,第二个点需要完成格式名称。
我相信在您的情况下您不需要%quote()
- 格式名称不能包含%quote()
引用的字符串。
编辑:再次没试过,仅根据我看到的代码你还需要更改CALL SYMPUT("classformat", format);
到CALL SYMPUTX("classformat", format);
CALL SYMPUTX()是CALL SYMPUT()的高级版本,它删除宏变量值中的尾随空白,而原始版本保留空白。实际上,这将与您的解决方案相同,只是更简单。 所以问题确实在格式名称和句点之间有额外的空白。
答案 1 :(得分:0)
不知道为什么这样做,而且vasja的想法不会,但问题显然是格式名称末尾的句号(或者可能是一些额外的空格?)。我更改了数据步骤以在SYMPUT调用之前添加句点:
data _null_;
set contents;
myformat=catt(format,'.');
if name="&variabl." then CALL SYMPUT("classformat", myformat);
run;