【问题标题】:Iterative %DO LOOP issue in SAS macro for automated emailing of site metrics用于自动通过电子邮件发送站点指标的 SAS 宏中的迭代 %DO LOOP 问题
【发布时间】:2020-05-16 10:51:00
【问题描述】:

我有一个名为“SampleReport”的站点特定报告指标的数据集,我希望通过电子邮件将其自己的指标以附件 PDF 形式发送给每个站点。 PDF 生成并通过电子邮件发送到各自的站点,但是在尝试将 ODS 文本添加到 PDF 时,我在定义其他宏时遇到问题(目前只有“电子邮件”作为宏变量保留,但我也想要“站点” '、'Site_Contact' 和 'mcnt_enrolled')。

下面是测试数据集和我的代码;我是 SAS 新手,如果有任何帮助/建议,我将不胜感激:

    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);

【问题讨论】:

  • 你的变量值是多少,例如 &wantsite、&cntsep 和 &cntsite?也许您的数据中有一个新行,其中站点的值为空白,例如,如果它来自 csv 并且 csv 在最后一行之后有换行符,则可能会发生这种情况
  • 我已经解决了终止 %DO 循环的 EMAIL5 问题,但是我在定义除“电子邮件”之外的其他宏变量时遇到了问题。

标签: email sas macros iteration do-loops


【解决方案1】:

电子邮件%DO 循环语句未正确终止。

    * cycle through emails;
    %do I=1 %to &EMAILN

应该是

    * cycle through emails;
    %do I=1 %to &EMAILN;

也许ODS HTML CLOSE 应该在循环之外。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-13
    • 2012-10-17
    • 2014-04-25
    • 2016-01-09
    相关资源
    最近更新 更多