Hi,
I have a program in SAS EG that runs different macros dependant on different variables ... the results should be emails going to different addresses
the basic structure is
-a series of macro variables using %LET
-then the definition of each macro - FROM, TO, SUBJECT etc .. for each email
-then the following datastep.
It all works and brings up no errors - EXCEPT that it always uses the last macro written %email7 .... or if I change the last one to be %email1 for example it will run that, even if the conditions match a different macro. I have tried various different approaches but this is the only one that doesn't error. Can you let me know how I can achieve this ?
data sendemail;
if (&Customer_Segment='R' or &Customer_Segment='U') then do;
%email1 ;
if (&Customer_Segment='S' and &Manager_Status= 'Managed') then do;
%email2 ;
if (&Customer_Segment='S' and (&Manager_Status= ('Vacant' or 'Unmanaged'))) then do;
%email3 ;
if (&Customer_Segment='P' and &Manager_Status= 'Managed') then do;
%email4 ;
if (&Customer_Segment='P' and (&Manager_Status= ('Vacant' or 'Unmanaged'))) then do;
%email5 ;
if (&Customer_Segment='C' and &Manager_Status= 'Managed') then do;
%email6 ;
if (&Customer_Segment='C' and (&Manager_Status= ('Vacant' or 'Unmanaged'))) then do;
%email7 ;
run;
One obvious glaring error is that when you have
then do;
you also need to have an
end;
Hi Paige
when I put end after each 'then do' I get the following error 7 times, 1 for each 'then do'
330 end;
___
161
ERROR 161-185: No matching DO/SELECT statement.
when I just put end after the last 'do then' (for %email7) I get the same error, but only once
could you perhaps show me what you mean ?
thanks
I don't think macros are conditional, ie it will run all regardless of where it is
if (&Customer_Segment in ('U', 'R') ) then do;
%email1 ;
end;
else if (&Customer_Segment='S' and &Manager_Status= 'Managed') then do;
%email2 ;
end;
else if (&Customer_Segment='S' and (&Manager_Status in ('Vacant' , 'Unmanaged')) then do;
%email3 ;
end;
I think you need to call the macro with call execute instead.
if (&Customer_Segment in ('R' , 'U') ) then call execute('%email1;');
else if (&Customer_Segment='S' and &Manager_Status= 'Managed') then call execute('%email2;') ;
You have some other logical issues in your code that isn't valid, so you need to revise those to use IN as above.
(&Manager_Status= ('Vacant' or 'Unmanaged')))
I would suggest doing one or two at a time and then adding in the IF statements iteratively to ensure it works as designed. I'm also assuming that the %email macros don't need any data from the input data set. If it does, this process needs to be revised.
There's also an argument for putting Manager_Status and Customer Segment into the macro parameters and moving all of this logic into your %email macro instead. It would make it more manageable I suspect, since your %email macro will contain all the logic for the emailing in one place where it's now split into multiple macros/programs.
@franph wrote:
Hi,
I have a program in SAS EG that runs different macros dependant on different variables ... the results should be emails going to different addresses
the basic structure is
-a series of macro variables using %LET
-then the definition of each macro - FROM, TO, SUBJECT etc .. for each email
-then the following datastep.
It all works and brings up no errors - EXCEPT that it always uses the last macro written %email7 .... or if I change the last one to be %email1 for example it will run that, even if the conditions match a different macro. I have tried various different approaches but this is the only one that doesn't error. Can you let me know how I can achieve this ?
data sendemail;
if (&Customer_Segment='R' or &Customer_Segment='U') then do;
%email1 ;
if (&Customer_Segment='S' and &Manager_Status= 'Managed') then do;
%email2 ;
if (&Customer_Segment='S' and (&Manager_Status= ('Vacant' or 'Unmanaged'))) then do;
%email3 ;
if (&Customer_Segment='P' and &Manager_Status= 'Managed') then do;
%email4 ;
if (&Customer_Segment='P' and (&Manager_Status= ('Vacant' or 'Unmanaged'))) then do;
%email5 ;
if (&Customer_Segment='C' and &Manager_Status= 'Managed') then do;
%email6 ;
if (&Customer_Segment='C' and (&Manager_Status= ('Vacant' or 'Unmanaged'))) then do;
%email7 ;
run;
What are your macros like? Do they contain datastep code, which can be run inside the datastep? If not, you will have to call them in a different way. For instance using call execute, e.g.
if (&Customer_Segment='R' or &Customer_Segment='U') then
call execute('%email1 ;');
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.