简体   繁体   中英

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() .

EDIT: Again not tried, just based on the code I see you also need to change CALL SYMPUT("classformat", format); to 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. 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?). I changed the data step to add the period before the SYMPUT call:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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