I am a newbie to SAS.
I have around 100 observations in a variable and i want to take them into macro variable. And then i want to use one name at a time with the loop. That means i am using loop in macro so i use name1 in 1 iteration, then name2 in 2 iteration and name3 in 3rd iteration and so on.
For ex: variable Name have 5 names
Smith
John
Sam
Michael
Chen.
When i run macro with parameter name and loop it for five iteration. Then it should take Smith in one iteration and John in second.
Thanks in advance.
What does your macro do?
Usually with this kind of problem it is easier to use simple data step logic, since the data step already 'loops' through the names in the Name variable.
though I agree with @RW9, I am curious as to see what your macro look like? 🙂
It should look like this:
%macro test(names);
%do i=2 %to 5;
data _null_;
FILE Mailbox TO = abc@gmail.com
subject=&names.;
PUT "Dear ALL";
PUT " My name is";
run;
%end;
%mend;
%test(names);
my objective is to send five e-mail to abc@gmail with Subject line with name varibale. First e-mail with subject Smith, second with John and so on.
Do not use macro variables for list data. That's what datasets are for.
%macro test(name);
filename mailbox email "abc@gmail.com" subject="&name";
data _null_;
file mailbox;
put "Dear ALL";
put " My name is &name";
run;
filename mailbox clear;
%mend;
data names;
input name $10.;
cards;
Smith
John
;
run;
data _null_;
set names;
call execute('%test(' !! trim(name) !! ');');
run;
Thanks KurtBremser.
IT worked file.
Could you please tell me syntax when i have to run for two variables.
@saslearning1 wrote:
Thanks KurtBremser.
IT worked file.
Could you please tell me syntax when i have to run for two variables.
Add a second parameter to the macro definition, and use accordingly in the macro body.
Create the control dataset with two corresponding variables, and expand the call execute accordingly.
If you need to build a matrix, create two separate datasets (one for each variable), and use proc sql to create the final control dataset as a cartesian product of those.
For more detailed help, supply your control data in a data step that creates the dataset, similar to my example. Use the "little running man" icon to post code, so that formatting and all characters are preserved as-is.
Look into CALL EXECUTE() instead.
You can call your macro directly from the data step without having to create macro variables or looping again.
PS. Your macro is not correct.
To make a macro first get working code for a single iteration.
Determine what's dynamic and make that into your macro variables.
If you're calling it for each name, then CALL EXECUTE is definitely the route to go.
Here's a fully worked example that would print a report for each age and sex combination in the data set.
https://gist.github.com/statgeek/beb97b1c6d4517dde3b2
@saslearning1 wrote:
Hi Draycut, Thanks for reply.
I am trying to send dynamic e-mail using SAS. where Name will be dynamic in E-mail body. So for this i am taking Name variable into a macro variable.
I will be sending 5 e-mails with the name change in every e-mail.
Is it always the same email, or will the content of the mail change for every recipient? And how is the content of the email created (text with inserts, or reports from datasets, or ....)?
recipient remains same. It is just the content that changes. and minute change like name.
Though, you can quite easily get the names into a macro variable like this
data have;
input Name$;
datalines;
Smith
John
Sam
Michael
Chen
;
proc sql noprint;
select Name into :names separated by ' '
from have;
quit;
%put &names.;
I never recommend doing this this way. Macro language is not the place to manipulate and process data. For instance, you say you 100 observations which will be names, how long do you think the longest macro variable can be? What about special characters? There are loads of questions which start popping up when doing these lists of data in macro language.
For this specific question, the simplest method would be to create (or get your IT group) to create a distribution list of all the email addresses, this list can be modified external from your code, added to, removed from, duplicated etc. and can be of any length. You code then just needs to reference the distribution list, simplifying both your coding, and list maintenance. It would also make finding emails within the email server easier - just find the distro list, could make access easier - i.e. company directories.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.
Find more tutorials on the SAS Users YouTube channel.