Email SAS宏中的迭代%DO循环问题,用于自动发送站点指标的电子邮件
我有一个名为“SampleReport”的网站特定报告指标数据集,我希望通过电子邮件将每个网站自己的指标附在PDF中。PDF生成并通过电子邮件发送到各自的站点,但是在尝试将ODS文本添加到PDF时,我在定义其他宏时遇到问题(目前只有“email”保留为宏变量,但我还需要“site”、“site\u Contact”和“mcnt\u registered”) 下面是测试数据集和我的代码;我是SAS的新手,非常感谢您的帮助/建议:Email SAS宏中的迭代%DO循环问题,用于自动发送站点指标的电子邮件,email,sas,macros,iteration,do-loops,Email,Sas,Macros,Iteration,Do Loops,我有一个名为“SampleReport”的网站特定报告指标数据集,我希望通过电子邮件将每个网站自己的指标附在PDF中。PDF生成并通过电子邮件发送到各自的站点,但是在尝试将ODS文本添加到PDF时,我在定义其他宏时遇到问题(目前只有“email”保留为宏变量,但我还需要“site”、“site\u Contact”和“mcnt\u registered”) 下面是测试数据集和我的代码;我是SAS的新手,非常感谢您的帮助/建议: site site_enrolled_total mc
site site_enrolled_total mcnt_enrolled Site_Contact Email Expected_Enrolled NetworkAvg Overall_Study_Enrollment Overall_Enrollment_to_Date
site_a 32 7 Homer Simpson hsimp@gmail.com 40 5 115 1518
site_b 36 8 Jim Schoe jimmy2schoes@aol.com 40 4 115 1518
site_ c 20 2 Hank Hill propaneking@yahoo.com 36 7 115 1518
site_d 27 7 Lisa Simpson lisasimpson@gmail.com 36 5 115 1518
这是我的代码:
%macro getemail(DATA);
ods _all_ close;
%let outdir = %sysfunc(getoption(WORK));
OPTIONS NODATE NONUMBER;
ODS GRAPHICS / RESET=ALL;
** get list of unique emails;
proc sql noprint;
select distinct(email) into :wantemail separated by '~'
from samplereport
order by email;
quit;
%put &=wantemail;
**count number of separators(i.e. '~') and count number of unique emails;
%let cntsep1 = %sysfunc(count(&wantemail,~));
%let cntemail = %eval(&cntsep1+1);
** start do loop to cycle thru list of emails;
** and create the site macro var for each loop;
%do i = 1 %to &cntemail;
%let email = %scan(&wantemail,&i,~);
%put This is for by group where email = &email;
**use site macro variable for file name and WHERE and TITLE;
ODS PDF (id=dagwood)
FILE="&outdir.\PDF_&email..PDF" STARTPAGE=NO STYLE=Journal gtitle;
title j=center bold "Metrics for January 2020";
ods text= "Email: &email. ";
ods text= "Site: &site. ";
ods text= "Site Contact: &Site_Contact.";
ods text= "Enrolled this Month: &mcnt_enrolled. ";
run;
ods layout gridded columns=2;
ods graphics / width=300 height=300;
ods region;
ods pdf(id=dagwood) style=statdoc;
PROC SGPLOT DATA=work.samplereport;
where email = "&email";
Title "Enrollment Metrics for Your Site";
vbar site / response=Expected_Enrolled stat=sum DATALABEL LEGENDLABEL="Expected Enrollment for Your Site"
discreteoffset=-0.5 barwidth=0.2 fillattrs=graphdata2;
yaxis grid display=(nolabel);
xaxis display=(noline NOTICKS NOVALUES nolabel);
vbar site / response=site_enrolled_total stat=sum DATALABEL LEGENDLABEL="Enrolled for Your Site to Date"
discreteoffset=-0.2 barwidth=0.2 fillattrs=graphdata1;
yaxis grid display=(nolabel);
xaxis display=(noline NOTICKS NOVALUES nolabel);
vbar site / response=mcnt_enrolled stat=sum DATALABEL LEGENDLABEL="Enrolled this Month"
discreteoffset=0.2 barwidth=0.2 fillattrs=graphdata3;
yaxis grid display=(nolabel);
xaxis display=(noline NOTICKS NOVALUES nolabel);
vbar site / response=NetworkAvg stat=sum DATALABEL LEGENDLABEL="Your Network Avg Enrollment this Month"
discreteoffset=0.5 barwidth=0.2 fillattrs=graphdata4;
yaxis grid display=(nolabel);
xaxis display=(noline NOTICKS NOVALUES nolabel);
RUN;
ods region;
ods pdf(id=dagwood) style=statdoc;
goptions reset=all;
proc sgplot data=work.samplereport;
where email = "&email";
Title "Study-Wide Enrollment to Date and Goal";
vbar site / response=Overall_Study_Enrollment stat=sum DATALABEL LEGENDLABEL="Enrollment for All Sites"
discreteoffset=-0.3 barwidth=0.4 fillattrs=graphdata5;
yaxis grid display=(nolabel);
xaxis display=(noline NOTICKS NOVALUES nolabel);
vbar site / response=Overall_Expected_Study stat=sum DATALABEL LEGENDLABEL="Expected Enrollment Goal"
discreteoffset=0.3 barwidth=0.4 fillattrs=graphdata6;
yaxis grid display=(nolabel);
xaxis display=(noline NOTICKS NOVALUES nolabel);
run;
ODS LAYOUT END;
%end;
* get emails from DATA;
proc sql noprint;
select EMAIL into :EMAIL1- from &DATA;
select count(*) into :EMAILN from &DATA;
* cycle through emails;
%do I=1 %to &EMAILN;
filename temp email to="&&EMAIL&I"
attach="&outdir.\PDF_&&email&I...PDF"
from="myemail@gmail.com"
type="text/html"
subject="Site Metrics";
ods html file=temp;
run;
ods html close;
%end;
%mend getemail;
%getemail(samplereport);
电子邮件
%DO
循环语句未正确终止
* cycle through emails;
%do I=1 %to &EMAILN
应该是
* cycle through emails;
%do I=1 %to &EMAILN;
也许
ODS HTML CLOSE
应该在循环之外。变量的值是多少,例如&wantsite、&cntsep和&cntsite?可能在您的数据中有一个新行的站点值为空,这可能会发生,例如,如果它来自csv,并且csv在最后一行之后有一个换行。我已通过终止%DO循环修复了EMAIL5问题,但是我在定义除“email”之外的其他宏变量时遇到问题。