01-05-2018 06:11 AM
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
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.
01-05-2018 06:16 AM
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.
01-05-2018 06:20 AM
01-05-2018 06:39 AM
It should look like this:
%do i=2 %to 5;
FILE Mailbox TO = email@example.com
PUT "Dear ALL";
PUT " My name is";
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.
01-05-2018 06:45 AM - edited 01-05-2018 06:46 AM
Do not use macro variables for list data. That's what datasets are for.
%macro test(name); filename mailbox email "firstname.lastname@example.org" 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;
01-05-2018 06:55 AM
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.
01-05-2018 10:29 AM
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.
01-05-2018 06:32 AM
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 ....)?
01-05-2018 06:18 AM
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.;
01-05-2018 06:26 AM
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.