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 ;');

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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