BookmarkSubscribeRSS Feed
franph
Calcite | Level 5

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;

 

 

 

4 REPLIES 4
PaigeMiller
Diamond | Level 26

One obvious glaring error is that when you have

 

then do;

 

you also need to have an

 

end;

--
Paige Miller
franph
Calcite | Level 5

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

Reeza
Super User

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. 

 

Spoiler

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

 

 

 


 

 

s_lassen
Meteorite | Level 14

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 ;');
How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1395 views
  • 0 likes
  • 4 in conversation