Hello everyone,
suppose I want to write a macto that generates the following line of code
data smthg
call missing(var1,var2,var3,var4)
run;
my best effort is this:
%macro my_missing;
data smtgh;
call missing (
%do i=1 %to 4;
%if i ne 4 %then
var&i,;
%else
var&i;
%end;
);
run;
%mend my_missing
when I run it I get the following error:
It's a good approach to first have some working data step code before you try and generate such code. Taking what you shared and after fixing the syntax errors this appears to work.
data smthg;
call missing(var1,var2,var3,var4);
run;
Now you just need to get the macro syntax right so it generates the exact same data step code. Use option MPRINT to have SAS write to the SAS log what the macro generates.
I'm using in below code also option mfile as this allows to write the macro generated code to an external file ...and then I've got a data _null_ step to print this files with the SAS code that gets generated and then executed.
filename mprint temp;
options mprint mfile;
%macro my_missing();
data smtgh;
call missing (
%do i=1 %to 4;
%if i ne 4 %then
var&i,;
%else
var&i;
%end;
);
run;
%mend my_missing;
%my_missing();
data _null_;
infile mprint;
input;
put _infile_;
run;
filename mprint temp;
options mprint mfile;
%macro my_missing();
data smtgh;
call missing (
%do i=1 %to 4;
%if i ne 4 %then
var&i,;
%else
var&i;
%end;
);
run;
%mend my_missing;
%my_missing();
And here what the macro generates that then throws the error.
You need to amend your macro so it doesn't generate this last comma.
Btw: If that's for some real problem you want to solve then eventually share with us the real problem. I have a suspicion there might be easier approaches to get to what you really need.
Hello @MikeFox,
Add the missing ampersand in the %IF condition
&i ne 4
and the missing semicolon of the %MEND statement and correct the typo in the dataset name. Then it will work.
Edit: In addition, I would insert
%local i;
before the DATA statement, as this is good practice to avoid name conflicts with macro variables outside the macro.
It's a good approach to first have some working data step code before you try and generate such code. Taking what you shared and after fixing the syntax errors this appears to work.
data smthg;
call missing(var1,var2,var3,var4);
run;
Now you just need to get the macro syntax right so it generates the exact same data step code. Use option MPRINT to have SAS write to the SAS log what the macro generates.
I'm using in below code also option mfile as this allows to write the macro generated code to an external file ...and then I've got a data _null_ step to print this files with the SAS code that gets generated and then executed.
filename mprint temp;
options mprint mfile;
%macro my_missing();
data smtgh;
call missing (
%do i=1 %to 4;
%if i ne 4 %then
var&i,;
%else
var&i;
%end;
);
run;
%mend my_missing;
%my_missing();
data _null_;
infile mprint;
input;
put _infile_;
run;
filename mprint temp;
options mprint mfile;
%macro my_missing();
data smtgh;
call missing (
%do i=1 %to 4;
%if i ne 4 %then
var&i,;
%else
var&i;
%end;
);
run;
%mend my_missing;
%my_missing();
And here what the macro generates that then throws the error.
You need to amend your macro so it doesn't generate this last comma.
Btw: If that's for some real problem you want to solve then eventually share with us the real problem. I have a suspicion there might be easier approaches to get to what you really need.
Thanks to you and to @FreelanceReinh for the tips. I'm not solving a real world problem, I was just messing around with the macro facility to get more comfortable with it and both your answers were very helpful.
Have a good day!
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.
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.
Ready to level-up your skills? Choose your own adventure.