简体   繁体   中英

SAS: If condition won't recognize macro variable within datastep

In the code below the IF statement that sends the email isn't evaluating correctly. I am not sure why. I tried to check for a null but that didnt work either. It is just always sending the the first do in that statement. In the below statement TABLE1 exist with no records and TABLE2 does not exist. I think it has something to do with &CNT3 being populated with a COUNT(*) in the proc sql statement.

%IF %SYSFUNC(exist(TABLE1)) %THEN %DO;

    PROC SQL;
        SELECT COUNT(*) INTO : CNT3 FROM TABLE1;
    QUIT;
    %END;
%ELSE %DO;

    %LET CNT3=0;
    %END;

    %put &cnt3.;

%IF %SYSFUNC(exist(TABLE2)) %THEN %DO;

    PROC SQL;
        SELECT COUNT(*) INTO : CNT4 FROM TABLE2;
    QUIT;
    %END;
%ELSE %DO;

    %LET CNT4=0;
    %END;
    %put &cnt4.;



%IF (&CNT3 ^=0 AND &CNT3^='0') %THEN %DO;

    PROC EXPORT DATA=TABLE1.
        DBMS=XLSX
        OUTFILE="data/REPORT1.xlsx"
        REPLACE;
        SHEET="TEST1";
    RUN;
%END;

%IF (&CNT4 ^=0 AND &CNT4^='0') %THEN %DO;

    PROC EXPORT DATA=&ENV..AUTH_ERRORLOG_&REC_DATE.
        DBMS=XLSX
        OUTFILE="data/REPORT1.xlsx"
        REPLACE;
        SHEET="TEST2";
    RUN;
%END;

%let EMAIL_SUBJECT = "TEST EMAIL.";

FILENAME OUTBOX EMAIL 'TEST@TEST.COM';

DATA _NULL_;
    IF (&CNT3 ^=0 AND &CNT3 ^='0') OR (&CNT4 ^=0 AND &CNT4^='0') THEN
        DO;
            FILE OUTBOX
                TO=('TEST@TEST.COM')
                SUBJECT= &EMAIL_SUBJECT.
                ATTACH=("/data/REPORT1.xlsx" CONTENT_TYPE="APPLICATION/XLSX");
            
        END;
        ELSE DO;

                FILE OUTBOX
                TO=('TEST@TEST.COM')
                SUBJECT= &EMAIL_SUBJECT.;
                PUT"NO ERRORS FOUND";
        END;
            

RUN;

There may be a number of things going on here, so let's try to clean this up a bit to see if it will resolve your issues.

First, let's grab the observation count from the metadata of the tables of interest instead of counting all the observations. This is a great repeatable macro that I highly recommend keeping as an always-available sasauto:

%macro nobs(data);
    %local dsid nobs rc;

    %let nobs = -1;

    %if(%sysfunc(exist(&data.)) ) %then %do;

        %let dsid = %sysfunc(open(&data.));
        %let nobs = %sysfunc(attrn(&dsid., nlobs));
        %let rc   = %sysfunc(close(&dsid.));
    %end;

    &nobs.
%mend;

This will act like a function and return the number of observations for a SAS table. If it does not exist, it returns -1. For example:

%put The number of obs in sashelp.cars is %nobs(sashelp.cars);
%put The number of obs in a non-existent table is %nobs(doesntexist);

Output:

The number of obs in sashelp.cars is 428
The number of obs in a non-existent table is -1

Now we're guaranteeing that we're always returning a number without spaces in it. Let's replace the program logic:

%if(%nobs(table1) > 0) %then %do;

    PROC EXPORT DATA=TABLE1
        DBMS=XLSX
        OUTFILE="data/REPORT1.xlsx"
        REPLACE;
        SHEET="TEST1";
    RUN;

%end;

%if(%nobs(table2) > 0) %then %do;

    PROC EXPORT DATA=&ENV..AUTH_ERRORLOG_&REC_DATE.
        DBMS=XLSX
        OUTFILE="data/REPORT1.xlsx"
        REPLACE;
        SHEET="TEST2";
    RUN;

%end;

%let EMAIL_SUBJECT = "TEST EMAIL.";

FILENAME OUTBOX EMAIL 'TEST@TEST.COM';

DATA _NULL_;
    IF (%nobs(table1) > 0 OR %nobs(table2) > 0) then do;
        FILE OUTBOX
             TO=('TEST@TEST.COM')
             SUBJECT= &EMAIL_SUBJECT.
             ATTACH=("/data/REPORT1.xlsx" CONTENT_TYPE="APPLICATION/XLSX");
            
        END;
            ELSE DO;
                    FILE OUTBOX
                    TO=('TEST@TEST.COM')
                    SUBJECT= &EMAIL_SUBJECT.;
                    PUT"NO ERRORS FOUND";
            END; 
RUN;

This test does not make any sense

&CNT3 ^=0 AND &CNT3 ^='0'

in either the macro logic or the data step logic.

If CNT3 is going to have values like 0 or 123 or even 123 then just test if it is zero or not:

&cnt3 ne 0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM