[英]SAS: put format in macro
我正在嘗試通過將格式分配給現有變量來創建新變量。 我正在從宏中執行此操作。 我收到以下錯誤:“:需要格式名稱。” 關於如何解決有什么想法? 謝謝!
/* 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 = ?);));
只是猜測,線
classvalue=put(class, %quote(&classformat.));
應該
classvalue=put(class, &classformat..);
兩點是因為宏處理器“吃”了宏變量名稱的結尾,第二點是完成格式名稱所需要的。 我相信您在大小寫格式中不需要%quote()
,格式名稱不能包含%quote()
引用的字符串。
編輯:再次沒有嘗試過,僅基於我看到的代碼,您還需要更改CALL SYMPUT("classformat", format);
CALL SYMPUTX("classformat", format);
CALL SYMPUTX()是CALL SYMPUT()的高級版本,它刪除宏變量值中的尾隨空格,而原始版本則保留空格。 實際上,這將與您的解決方案相同,只是更簡單。 因此,問題的確實是格式名稱和句點之間有多余的空格。
不知道為什么這樣做有效,而vasja的想法無效,但問題顯然出在格式名稱末尾的句點(或可能還有一些空白?)。 我更改了數據步驟,以在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.