我有169个城镇,我想迭代一个宏。我需要使用town-name(而不是城镇代码)保存输出文件。我有一个带有城镇代码和城镇名称的数据集(TOWN)。是否可以为每个迭代设置一个%let语句设置为town-name,其中i = town-code?
我知道我可以使用索引函数列出城镇名称,但是我想要一种设置索引函数的方法,以便在i = TOWN时将%let语句设置为TOWN.town-name .town代码。
以下所有答案似乎都有可能。我现在使用%let =%scan(,& i)选项。一个限制是城镇名称可以是多个单词,所以我用下划线代替我后来纠正的空格。
这是我的宏。我为169个城镇中的每一个输出了proc报告。我需要将excel文件保存为城镇的名称,并将标题包含在城镇的名称中。然后,在excel中,我将所有169个工作表合并为一个工作簿。
%MACRO BY_YEAR;
%let townname=Andover Ansonia Ashford Avon ... Woodbury Woodstock;
%do i = 1999 %to 2006;
%do j = 1 %to 169;
%let name = %scan(&townname,&j);
ods tagsets.msoffice2k file="&ASR.\Town_Annual\&i.\&name..xls" style=minimal;
proc report data=ASR nofs nowd split='/';
where YR=&i and TWNRES=&j;
column CODNUM AGENUM SEX,(dths_sum asr_sum seasr_sum);
define CODNUM / group ;
define agenum / group ;
define sex / across ;
define dths_sum / analysis ;
define asr_sum / analysis ;
define seasr_sum / analysis ;
break after CODNUM / ul;
TITLE1 "&name Resident Age-Specific Mortality Rates by Sex, &i";
TITLE2 "per 100,000 population for selected causes of death";
run;
ods html close;
%end;
%end;
%MEND;
答案 0 :(得分:2)
我的猜测是你想要通过城镇索引查找城镇名称的原因是重复调用每个城镇名称的宏。如果是这种情况,那么您甚至根本不需要参与城镇指数业务。只需使用每个城镇名称调用宏。有很多方法可以做到这一点。这是使用call execute()
的一种方式。
data towns;
infile cards dlm=",";
input town :$char10. @@;
cards;
My Town,Your Town,His Town,Her Town
;
run;
%macro doTown(town=);
%put Town is &town..;
%mend doTown;
/* call the macro for each town */
data _null_;
set towns;
m = catx(town, '%doTown(town=', ')');
call execute(m);
run;
/* on log
Town is My Town.
Town is Your Town.
Town is His Town.
Town is Her Town.
*/
如果您确实需要进行表查找,那么一种方法是将您的城镇名称转换为数字格式,并在给定索引值的情况下编写一个简单的宏来检索名称。类似的东西:
data towns;
infile cards dlm=",";
input town :$char10. @@;
cards;
My Town,Your Town,His Town,Her Town
;
run;
/* make a numeric format */
data townfmt;
set towns end=end;
start = _n_;
rename town = label;
retain fmtname 'townfmt' type 'n';
run;
proc format cntlin=townfmt;
run;
%macro town(index);
%trim(%sysfunc(putn(&index,townfmt)))
%mend town;
%*-- check --*;
%put %town(1),%town(2),%town(3),%town(4);
/* on log
My Town,Your Town,His Town,Her Town
*/
答案 1 :(得分:0)
或者您如何将代码和名称作为参数传递给宏?喜欢这个?
%MACRO DOSTUFF(CODE=, NAME=);
DO STUFF...;
PROC EXPORT DATA=XYZ OUTFILE="&NAME."; RUN;
%MEND;
DATA _NULL_;
SET TOWNS;
CALL EXECUTE("%DOSTUFF(CODE=" || STRIP(CODE) || ", NAME=" || STRIP(NAME) || ");");
RUN;