DATA Step, Macro, Functions and more

Use a substring with a if/else function within a macro

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

Use a substring with a if/else function within a macro

Hi All,

 

I'm very new to SAS Base so forgive if this question seems basic. I've created a Macro that emails a PowerPoint file to a list of emails. The list will contain at most 8 email addresses and are inputted into the Macro via Macro variables and will be of the form email_1="John.Smith@abc.ie", (note the "" are required). If only a few emails are required then the email arguments are left blank, i.e. email_2 = 

 

A colleague mentioned that a security measure should be put on the list of emails such that if the email address is external, i.e. not "@abc.ie" or (blank) then the Macro should not run or the PowerPoint should not be emailed to these emails. What I tried to do was the following within the Macro:

 

/*Note this is repeated 8 times for each macro email variable input*/

%if substr(&email_1.,length(&email_1.)-5,6) not in ('abc.ie', '') %then %do;
%let email_1 = '';
%put &email_1;
%end;

 

I.e. if the email is not blank or is not an abc.ie. email then replace the email variable with a blank.

 

However when I run the above I get the following error:

 

ERROR: Required operator not found in expression: substr(&email_1.,length(&email_1.)-5,6) %not %in ('abc.ie', '') 

 

Any ideas on what the problem could be? I've tried a number of variations and done some Googling but have had no luck. If this is the wrong place to post then please let me know. Also not the internal email address will always end in '@abc.ie'.

 

If you need some data, let me know and I an create some dummy data.

 

Kind Regards,

 

Lorcan

 

 


Accepted Solutions
Solution
‎02-22-2018 01:09 PM
Super User
Posts: 6,644

Re: Use a substring with a if/else function within a macro

Posted in reply to Ltsmash89

You're starting in a touch place, both sending emails and using macro language to do it.  Some of the principles of macro language that you are encountering ... macro language does not process data and does not (without special handling) execute DATA step functions.  It can execute macro language functions, which is all you need for this particular purpose (so we don't need to talk about what "special handling" means).  And second, macro language assumes that strings are strings, and so does not require quotes.  To apply this to your program, this would be an appropriate revision:

 

%if %substr(&email_1.,%length(&email_1.)-5,6) ne abc.ie %then %do;

 

There are a variety of ways to check whether a macro variable contains a null value, and it wasn't clear to me if you wanted to check for a null value or for two quotes.  So I removed that piece.  You can add it back in, but do not use the IN operator in macro language.  That also requires special handling.  So a possibility would be:

 

%if %substr(&email_1.,%length(&email_1.)-5,6) ne abc.ie and %length(&email_1.) > 0 %then %do;

 

 

View solution in original post


All Replies
Solution
‎02-22-2018 01:09 PM
Super User
Posts: 6,644

Re: Use a substring with a if/else function within a macro

Posted in reply to Ltsmash89

You're starting in a touch place, both sending emails and using macro language to do it.  Some of the principles of macro language that you are encountering ... macro language does not process data and does not (without special handling) execute DATA step functions.  It can execute macro language functions, which is all you need for this particular purpose (so we don't need to talk about what "special handling" means).  And second, macro language assumes that strings are strings, and so does not require quotes.  To apply this to your program, this would be an appropriate revision:

 

%if %substr(&email_1.,%length(&email_1.)-5,6) ne abc.ie %then %do;

 

There are a variety of ways to check whether a macro variable contains a null value, and it wasn't clear to me if you wanted to check for a null value or for two quotes.  So I removed that piece.  You can add it back in, but do not use the IN operator in macro language.  That also requires special handling.  So a possibility would be:

 

%if %substr(&email_1.,%length(&email_1.)-5,6) ne abc.ie and %length(&email_1.) > 0 %then %do;

 

 

New Contributor
Posts: 3

Re: Use a substring with a if/else function within a macro

Posted in reply to Astounding
Hi Super User. It worked. Made some edits by dropping the ""'s and implemented your code and it worked. Thanks so much
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 76 views
  • 0 likes
  • 2 in conversation