BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
MikeFox
Fluorite | Level 6

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: 

 

69 ); run;
_
159
 
ERROR 159-185: Null parameters for MISSING are invalid.
 
I know there are different and less convoluted ways to achieve the same practical result, but I'm trying to explore the macro language and see if I can dinamically create a set of arguments for a function or a call routine when commas are required to separate each agument. 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

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;

Patrick_0-1675164334264.png

 

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. 

Patrick_1-1675164569315.png

 

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.

View solution in original post

3 REPLIES 3
FreelanceReinh
Jade | Level 19

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.

Patrick
Opal | Level 21

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;

Patrick_0-1675164334264.png

 

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. 

Patrick_1-1675164569315.png

 

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.

MikeFox
Fluorite | Level 6

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!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 462 views
  • 2 likes
  • 3 in conversation