BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Xingr_92
Calcite | Level 5

 Hello community,

I am a Marco beginner and trying to create a macro variable with 3 positional parameters. The purpose for doing so is to only change positional parameters then manage different datasets.

Here is the SAS code:

%macro dentalproc(vars1,vars2,vars3); /*create a marco with 3 positional parameters*/

data dnt_&vars1_1;/* 1st parameter used here to create a new dataset*/

set dnt_pnl.h&vars2;/* 2nd parameter used here to call an existing dataset*/

if panel ^=13 then delete;

run;

data dnt_&vars1_2;/* 1st parameter used here to create a new dataset with a different name*/

set dnt_pnl.h&vars3;/* 3rd parameter used here to call another existing dataset*/

if panel ^=13 then delete;

run;

%mend dentalproc;

%dentalproc(p14,126b,135b)

However, I keep receiving ERROR 22-322 when I ran this code.

ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string, (, /, ;, _DATA_, _LAST_, _NULL_.

 

ERROR 200-322: The symbol is not recognized and will be ignored.

Please, kindly advice the solutions. Thanks for your time and support!

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

You have more messages than the ones you gave us.

VERY STRONG HINT:

ALL messages in the log are important. ALL. Every single one of them. In your case, you have a WARNINGs for apparent symbolic references:

WARNING: Apparent symbolic reference VARS1_1 not resolved.

WARNING: Apparent symbolic reference VARS1_2 not resolved.

This happens because you did not terminate your macro variable reference with a dot (which is always recommended, see Maxim 48).

The statements should be

data dnt_&vars1._1;

and

data dnt_&vars1._2;

Fixing those will most likely deal with the syntax errors.

View solution in original post

4 REPLIES 4
Kurt_Bremser
Super User

You have more messages than the ones you gave us.

VERY STRONG HINT:

ALL messages in the log are important. ALL. Every single one of them. In your case, you have a WARNINGs for apparent symbolic references:

WARNING: Apparent symbolic reference VARS1_1 not resolved.

WARNING: Apparent symbolic reference VARS1_2 not resolved.

This happens because you did not terminate your macro variable reference with a dot (which is always recommended, see Maxim 48).

The statements should be

data dnt_&vars1._1;

and

data dnt_&vars1._2;

Fixing those will most likely deal with the syntax errors.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Are you really sure you want positional parameters in your macro?

The reason being is that if the end user messes up the order, your code may work correctly but do something unexpected.

Consider (and note the semicolon which was missing in your call):

%dentalproc(135b,p14,126b);     

 This would produce a very different result.  This type of thing can lead to plenty of debugging.  Remember that code you write will likely be read by people (possibly even yourself when you have forgotten it) in the future, therefore the simpler, easier to read you make your code now, the happier anyone looking at it in the future will be.

%macro dentalproc (input_ds1=,
                   input_ds2=, 
output_ds=); data &output_ds.1 &output_ds.2; set &input_ds1. (in=a)
&input_ds2. (in=b); where panel=13;
if a then output &output_ds.1;
else output &output_ds.2; run; %mend dentalproc; %dentalproc (input_ds1=dnt_pnl.p14,
input_ds2=dnt_pnl.126b,
output_ds=135b);

So I give an example (may or may not meet your needs, just going on what is here).  Named parameters, so its clear what each one is used for.  I would also put the libname in the call rather than in the code, as then the macro is more flexible - you want to make macros as felixible as possible whilst keeping them simple.

Tom
Super User Tom
Super User

One of the nice features of SAS macro code is that you can define parameters to allow call by position, but still in the call use the names.  Just remember that call by position values must come first in both the definition and the call. In the macro call you can call the parameters by name in any order you want.

%macro dentalproc 
(input_ds1
,input_ds2
,output_ds
); 

%dentalproc(input_ds1=135b,output_ds=p14,input_ds2=126b)

Also note that adding a semi-colon after the call to a macro call that specifies parameters is NOT necessary.  The macro processor knows that you have ended the macro call when it sees the end of the parameter list.

 

When the macro generates only part of a statement then you need to be extra careful about adding a semi-colon.  Only add the semi-colon at the end of the full statement that is being partially generated by the macro call. 

%let estimated_size = %eval( %nobs(mydata) * 10000 );

Adding the semi-colon after a macro like this that generates statements does not really hurt in most situations since it will normally just result in an extra empty statement.  But note that if the macro call does not provide parameters (that is no parentheses) and the macro does have parameters defined then you need to add something to let the SAS parser know that the macro call has ended.  So either more code you want to run or add and empty parameter list by just adding ( )  or just add that extra semi-colon.

ballardw
Super User

You will want to learn some of the macro debugging options such as MPRINT SYMBOLGEN and/ or MLOGIC.

 

If you set options MPRINT before a macro call executes you will get the code generated by the macro and error messages will likely make more sense as they will be in relation to the generated code.

 

options mprint;

%dentalproc(p14,126b,135b);

options nomprint;

Symbolgen show the substitution/resolution of macro variables and can be quite helpful if a macro variable is resolving in an unexpected manner. Mlogic shows the results logic statements such as %if .

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
  • 4 replies
  • 821 views
  • 1 like
  • 5 in conversation