0

我正在尝试通过为现有变量分配格式来创建新变量。我正在从宏中执行此操作。我收到以下错误:“:需要格式名称。” 关于如何解决的任何想法?谢谢!

/* 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 = ?);)); 
4

2 回答 2

1

只是猜测,线

classvalue=put(class, %quote(&classformat.));

应该

classvalue=put(class, &classformat..); 

两点,因为一是被宏处理器“吃掉”以标记宏变量名称的结尾,二是需要完成格式名称。我相信你不需要%quote()在你的情况下 - 格式名称不能包含由%quote().

编辑:再次没有尝试,只是基于我看到的代码你还需要更改CALL SYMPUT("classformat", format);CALL SYMPUTX("classformat", format);

CALL SYMPUTX() 是 CALL SYMPUT() 的高级版本,它删除宏变量值中的尾随空格,而原始版本保留空格。实际上,这将与您的解决方案相同,只是更简单。所以问题确实是格式名称和句点之间有多余的空格。

于 2013-01-29T09:56:04.803 回答
0

不知道为什么这行得通,而 vasja 的想法却行不通,但问题显然在于格式名称末尾的句点(或者可能是一些额外的空白?)。我更改了数据步骤以在 SYMPUT 调用之前添加句点:

data _null_; 
set contents; 
myformat=catt(format,'.'); 
if name="&variabl." then CALL SYMPUT("classformat", myformat); 
run; 
于 2013-01-29T15:56:18.957 回答