Quantcast
Channel: Active questions tagged email - Stack Overflow
Viewing all articles
Browse latest Browse all 29904

Iterative %DO LOOP issue in SAS macro for automated emailing of site metrics

$
0
0

I have a dataset titled 'SampleReport' of site specific reporting metrics and am looking to email each site their own metrics in an attached PDF. The PDFs generate well, and were emailing out to their respective sites yesterday, however the iterative %DO loop now appears to expect a 5th email address where there are only 4 sites in my test dataset (Warning Code: Apparent symbolic reference EMAIL5 not resolved).

Below is the test dataset and my code; I'm new to SAS and would appreciate any help/suggestions:

    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

And here's my code:

    %macro getemail(DATA);
    OPTIONS NODATE NONUMBER;
    ODS GRAPHICS / RESET=ALL;

    ** get list of unique sites;
    proc sql noprint;
        select distinct(site) into :wantsite separated by '~'
            from samplereport
                order by site;
    quit;

    %put &=wantsite;

    **count number of separators(i.e. '~') and count number of unique sites;
    %let cntsep = %sysfunc(count(&wantsite,~));
    %let cntsite = %eval(&cntsep+1);

    ** start do loop to cycle through list of sites;
    ** and create the site macro var for each loop;
    %do i = 1 %to &cntsite;
        %let site = %scan(&wantsite,&i,~);
        %put This is for by group where site = &site;
        ODS PDF (id=dagwood)
            FILE="&outdir.\PDF_&site..PDF" STARTPAGE=NO STYLE=Journal gtitle;

        proc print data=work.samplereport NOOBS;
            where site = "&site";
            title "THESE ARE YOUR SITE'S ENROLLMENT METRICS FOR THIS PAST MONTH";
            var site    email   site_enrolled_total    mcnt_enrolled    Expected_Enrolled   NetworkAvg
                Overall_Study_Enrollment    Overall_Expected_Study;
        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 site = "&site";
        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 site = "&site";
        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_&site...PDF"
        from="myemail@gmail.com"
            type="text/html"
            subject="Site Metrics";
            ods html file=temp;
            where ="&&&EMAIL&I";
    run;

    ods html close;
        %end;
%mend getemail;

%getemail(samplereport);

Viewing all articles
Browse latest Browse all 29904

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>