how to integrate a SAS code to send an automatic e-mail?

70 views Asked by At

I have two SAS codes 1 and 2, I want to merge the two codes to send an automatic email from SAS, I want to replace the put of the second code (PUT "Code1???????????????? ") by that of the first, I do not know how to do that, can you help me?

/**********Code1********/
%macro Controle_QDD (Branche);
data SITMVT&Branche.;
    set Stock&Branche.;
    merge Stock&Branche. (in=A) Base&Branche. (in=B);
    by  &Varlist.
    ;

    if A and not B;
run;
 proc sql noprint;
        select count(*) as NB&Branche.
        from SITMVT&Branche.;
    quit;
    %if NB&Branche. > 0  %then
        %put Le contrôle QDD est OK pour SITMVT&Branche. ;
    %else
        %put Le contrôle est KO pour SITMVT&Branche. Je vous invite à vérifier les résultats en exécutant le code  ;

%mend;
%Controle_QDD(B);
%Controle_QDD(L);

/******Code2********/
filename mymail email FROM= "XXX"
TO=(
"XXX"

)
Cc=("XXX"

)
        TYPE="text/html"
        CONTENT_TYPE="text/html";
DATA _null_;
    FILE mymail
         SUBJECT="Controle QDD ";
    
    PUT 'Bonjour,';
    PUT '<br>';
    PUT '<br>';
    PUT '<br>';

    PUT "Code1???????????????? "; /**** replace by  Code 1 ????****/

    PUT '<br>';
    PUT 'Pour avis :-)';
    PUT '<br>';
    PUT '<br>';
    PUT '<i>Note: ce mail est généré automatiquement, merci de ne pas y répondre.</i> ';
    PUT '<br>';
    PUT '<Cordialement,>';
RUN;

```---------------------------------
2

There are 2 answers

4
Chris Long On

Your 'Code 1' won't work as it is. It looks like you're trying to create a macro variable called NB&Branche in the SQL step, but your existing code is only creating a temporary SQL variable. You need:

proc sql noprint;
  select count(*) into :NB&Branche from SITMVT&Branche;
quit;

to store the value in to a macro variable. Then your following macro IF statements need to resolve the value of that macro variable like this (note the extra && at the beginning):

%if &&NB&Branche > 0  %then
  %put Le contrôle QDD est OK pour SITMVT&Branche. ;
%else
  %put Le contrôle est KO pour SITMVT&Branche. Je vous invite à vérifier les résultats en exécutant le code  ;

And you can basically just repeat that part in your 'Code 2':

PUT '<br>';

%let branche = B;
%if &&NB&Branche > 0  %then
   PUT "Le contrôle QDD est OK pour SITMVT&Branche.";
%else
   PUT "Le contrôle est KO pour SITMVT&Branche. Je vous invite à vérifier les résultats en exécutant le code";

%let branche = L;
%if &&NB&Branche > 0  %then
   PUT "Le contrôle QDD est OK pour SITMVT&Branche.";
%else
   PUT "Le contrôle est KO pour SITMVT&Branche. Je vous invite à vérifier les résultats en exécutant le code";

PUT '<br>';
PUT 'Pour avis :-)';

to conditionally add one message or the other to your email. The whole thing could be made a lot neater - you could save the required message for each 'branche' to a macro variable during 'Code 1', for example - but that should be enough to get it working...

Bon chance!

0
Tom On

So your main issue is the attempt to use %IF in open code. If you really need that then wrap that code in another macro definition and call that macro.

But since you are running in a DATA step just use normal IF statements instead.

DATA _null_;
    FILE mymail
         SUBJECT="Controle QDD"
    ;
    PUT 'Bonjour,'
      / '<br><br><br>'
    ;
    PUT '<br>';
    if &NBB > 0  then PUT
       "Le contrôle QDD est OK pour SITMVTB."
    ;
    else PUT
       "Le contrôle est KO pour SITMVTB. "
       "Je vous invite à vérifier les résultats en exécutant le code"
    ;
    if &NBL > 0  then PUT
       "Le contrôle QDD est OK pour SITMVTL."
    ;
    else PUT 
       "Le contrôle est KO pour SITMVTL. "
       "Je vous invite à vérifier les résultats en exécutant le code"
    ;
    PUT '<br>'
      / 'Pour avis :-)'
      / '<br>'
      / 'Pour avis :-)'
      / '<br>'
      / '<br>'
      / '<i>Note: ce mail est généré automatiquement, '
        'merci de ne pas y répondre.</i>'
      / '<br>'
      / '<Cordialement,>'
   ;
RUN;