Building email address using %let statement

Accepted Solution Solved
Reply
Contributor
Posts: 20
Accepted Solution

Building email address using %let statement

[ Edited ]

Hello,

 

I am trying to build an email address macro variable for use with the 'emailid' system option for sending email from a SAS program, using the user's network ID (as returned by the automatic macro variable _clientuserid). The program in question may be run by various users and I don't want anyone to have to hard code their email address in the program.

 

I've modified the macro variable code every way I can think of, but except for building it with two macro vars, I always end up with the variable not resolving correctly in the options statement. Is it possible to do it in one %let statement? I feel there must be one simple thing I'm overlooking.

 

Here is my code that works using two %let statements:

 

%let my_login = %sysfunc(compress(&_clientuserid.'@smud.org',"'"));
%let sender = "&my_login";

options emailsys=smtp emailhost=<oursystem> emailport = <ourport> emailed=&sender.;

filename mymail email to=<recipients> subject="SAS test email";
data _null_;
file mymail;
put "This is a test";
run;

 

I am running SAS EG 5.1, processing on a UNIX server.

 

Tamara

 


Accepted Solutions
Solution
‎09-21-2016 01:59 PM
Super User
Posts: 17,730

Re: Building email address using %let statement

Not sure this is better than your current solution. 

I would use dequote + quote functions.

 

You could probably nest your functions as well, using another %sysfunc and the quote function. 

 

%let _clientuserid='awesome';
%let my_login = %sysfunc(quote(%sysfunc(dequote(&_clientuserid))@smud.org));
%put &my_login;

View solution in original post


All Replies
Super User
Posts: 17,730

Re: Building email address using %let statement

Unless I'm missing something, isn't it:

 

 

%let _clientuserid=awesome;
%let my_login = "&_clientuserid.@smud.org";
%put &my_login;
Contributor
Posts: 20

Re: Building email address using %let statement

Reeza,

 

Thank you for responding.

 

The system macro variable _clientuserid produces the userid enclosed in single quotes (e.g. 'tbarker'). To mimic this in your code, you would need to put single quotes around the word 'awesome'. Your second let statement would then produce "'awesome'@smud.org", retaining the single quotes around 'awesome'. That causes an error when used in the emailid option.

 

I, too, thought it should be as easy as %let my_login = "&_clientuserid.@smud.org";, but it's the embedded single quotes in the _clientuserid return value that cause the problem, requiring the compress function to get rid of them. And when I try to enclose the my_login value (as I coded it) in double quotes using a variety of methods, SAS produces various errors. Thus the second let statement to enclose the value from the first let statement in double quotes.

Solution
‎09-21-2016 01:59 PM
Super User
Posts: 17,730

Re: Building email address using %let statement

Not sure this is better than your current solution. 

I would use dequote + quote functions.

 

You could probably nest your functions as well, using another %sysfunc and the quote function. 

 

%let _clientuserid='awesome';
%let my_login = %sysfunc(quote(%sysfunc(dequote(&_clientuserid))@smud.org));
%put &my_login;
Super User
Posts: 17,730

Re: Building email address using %let statement

And another option, move the quotes to the options statement and have no quotes in your macro variable.

 

 

Contributor
Posts: 20

Re: Building email address using %let statement

[ Edited ]

Quote/Dequote worked! Now it's just one let statement instead of two. Not a critical requirement, but I like to have as efficient code as possible. I'm sure I had tried the dequote function before, but I was messing with the code so much that I never got it to work in whichever iteration I tried it. And I for some reason had not tried the quote function. Sometimes you just need someone else to come at your problem with a clearer mind. Thank you for your help!

 

☑ This topic is SOLVED.

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

Discussion stats
  • 5 replies
  • 374 views
  • 0 likes
  • 2 in conversation