- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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:
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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!