简体   繁体   English

SAS:将格式放入宏

[英]SAS: put format in macro

I am trying to create a new variable by assigning a format to an existing variable. 我正在尝试通过将格式分配给现有变量来创建新变量。 I'm doing this from within a macro. 我正在从宏中执行此操作。 I'm getting the following error: ": Expecting a format name." 我收到以下错误:“:需要格式名称。” Any thoughts on how to resolve? 关于如何解决有什么想法? Thanks! 谢谢!

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

Just guessing, the line 只是猜测,线

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

should be 应该

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

Two points because one is "eaten" by macro processor to mark end of macro variable name, the second one is needed to complete format name. 两点是因为宏处理器“吃”了宏变量名称的结尾,第二点是完成格式名称所需要的。 I believe you won't need %quote() in your case - format name cannot contain strings quoted by %quote() . 我相信您在大小写格式中不需要%quote() ,格式名称不能包含%quote()引用的字符串。

EDIT: Again not tried, just based on the code I see you also need to change CALL SYMPUT("classformat", format); 编辑:再次没有尝试过,仅基于我看到的代码,您还需要更改CALL SYMPUT("classformat", format); to CALL SYMPUTX("classformat", format); CALL SYMPUTX("classformat", format);

CALL SYMPUTX() is advanced version of CALL SYMPUT(), it removes trailing blanks in macro variable value while the original version keeps blanks. CALL SYMPUTX()是CALL SYMPUT()的高级版本,它删除宏变量值中的尾随空格,而原始版本则保留空格。 Effectively this will be same as your solution, just simpler. 实际上,这将与您的解决方案相同,只是更简单。 So the problem is indeed with extra blanks between format name and the period. 因此,问题的确实是格式名称和句点之间有多余的空格。

No idea why this works and vasja's idea wouldn't, but the problem was clearly with the period on the end of the format name (or perhaps some extra white space?). 不知道为什么这样做有效,而vasja的想法无效,但问题显然出在格式名称末尾的句点(或可能还有一些空白?)。 I changed the data step to add the period before the SYMPUT call: 我更改了数据步骤,以在SYMPUT调用之前添加句点:

data _null_; 
set contents; 
myformat=catt(format,'.'); 
if name="&variabl." then CALL SYMPUT("classformat", myformat); 
run; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM