DATA Step, Macro, Functions and more

difference between parameter macro and positional macro

Reply
Frequent Contributor
Posts: 76

difference between parameter macro and positional macro

Kindly explain me the difference between positional macro and parameter macro.

Thanks,

Regards,

Ashwini

PROC Star
Posts: 7,363

difference between parameter macro and positional macro

You can read about them at: http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/viewer.htm#macro-stmt.htm

Primarily it is the diffference between identifying the variables passed to the macro either by position or by a keyword name followed by an = sign.

Super User
Super User
Posts: 6,502

difference between parameter macro and positional macro

When you define the parameters to your macro you can make one or more of the parameters positional.  All positional parameters must come at the beginning of the parameter list, preceding any keyword parameters.

You should only define parameters as positional when there usage is obvious from the purpose of the macro. 

When you call the macro you can specify the value for the positional parameters without using their names.

The advantage of the keyword parameters is that you can specify a defualt value in the macro definition.

One of the user friendly features of SAS macro language is that when you call the macro you can reference any parameter using the keywords, whether the parameter is defined as positional or keyword.  When you call a macro using the keywords you can specify the parameters in any order. 

Super User
Posts: 9,682

difference between parameter macro and positional macro

I prefer to use positional macro variable.

Because it can let you see both name of macro variable and value of macro variable, whereas parameter macro variable only can see value of macro variable. when you use this macro.

Ksharp

Super User
Super User
Posts: 6,502

Re: difference between parameter macro and positional macro

Actually with SAS macros you can use the parameter names in the calls whether they are defined as positional or keyword.  Here is an example:

%macro square(x);

   %sysevalf(&x*&x)

%mend;

%put The square of 4 is %square(x=4) ;

Super User
Posts: 9,682

Re: difference between parameter macro and positional macro

Hi. Tom.

Actually it is also positional (Keyword) macro variable. You just use another macro variable to replace it. It will add some more unreadable thing.

%macro square(y);

   %sysevalf(&y*&y)

%mend;

%put The square of 4 is %square(x=4) ;

Ksharp

Valued Guide
Posts: 2,175

Re: difference between parameter macro and positional macro

I am unfamiliar with the term "parameter macro".

Parameter types I understand are Positional or Keyword.

Positional parameters cannot be defined with default values, which I normally expect to use.

Except - in the case of macro functions when the positional parameter is (almost) always present, and probably needs none of the "description" that the keyword of a keyword parameter provides.

For example:

macro called %PATH() provides the physical path of a logical reference

%put %path( sasuser ) ;

I consider to be self documenting, just as Tom's %square() needs no keyword for that parameter X.

Of course the world always has exceptions.

What XXXXXX makes this macro statement valid

%macro trouble( ok=1, trouble=999, real_trouble ) / XXXXXX ;

???????????????????????

try

parmbuf

PROC Star
Posts: 7,363

Re: difference between parameter macro and positional macro

Peter, I presume that you meant parmbuff, but that doesn't allow that macro to compile.  I couldn't find any option that did.  The only way I could get it to run was by changing the spelling and using an old style macro to turn the positional parameter into a keyword parameter.

macro rtrouble rtrouble=%

%macro trouble( ok=1, trouble=999, rtrouble )/des='trouble';

%mend;

Super User
Super User
Posts: 6,502

Re: difference between parameter macro and positional macro

Perhaps he meant in the CALL and not the definition.

%macro trouble( ok=1, trouble=999 ) / parmbuff ;

data _null_;

  length mvar $32 value $200;

  do mvar='ok','trouble','syspbuff';

     value = symget(mvar);

     put (mvar value) (=);

  end;

run;

%mend trouble;

%trouble(rtrouble);

mvar=ok value=1

mvar=trouble value=999

mvar=syspbuff value=(rtrouble)

Valued Guide
Posts: 2,175

Re: difference between parameter macro and positional macro

Tom and Art

does this snip from my log explain?

1816  %macro trouble( ok=1, trouble=999, real_trouble ) / XXXXXX ;

ERROR: All positional parameters must precede keyword parameters.

ERROR: Extraneous information on %MACRO statement ignored.

ERROR: A dummy macro will be compiled.

1817  %macro trouble( ok=1, trouble=999, real_trouble ) / parmbuf ;

1818  %macro trouble( ok=1, trouble=999, real_trouble ) / parmbuf ;

compiler message goes away with XXXXX replaced by PARMBUF

Of course, there might be trouble if I don't define and use the macro correctlySmiley Wink

peter

(notice that my /parmbuf has only one "f")

PROC Star
Posts: 7,363

Re: difference between parameter macro and positional macro

I liked my old-style macro substitution better!  Will you be at SGF this year?

Trusted Advisor
Posts: 1,300

Re: difference between parameter macro and positional macro

%macro square /parmbuff;

%do i=1 %to %sysfunc(countw(&syspbuff));

  %let x=%scan(&syspbuff,&i);

  %put The square of &x is %sysevalf(&x*&x);

%end;

%mend;

%square(2,3,4);

The square of 2 is 4

The square of 3 is 9

The square of 4 is 16

Peter, I cannot seem to get your example to work?

ERROR: All positional parameters must precede keyword parameters.

ERROR: Extraneous information on %MACRO statement ignored.

ERROR: A dummy macro will be compiled.

14         %macro square(act=,trouble) /parmbuf;

15          %if &act=y %then

16           %do;

17            %do i=3 %to %sysfunc(countw(&syspbuff));

18             %let x=%scan(&syspbuff,&i);

19             %put The square of &x is %sysevalf(&x*&x);

20            %end;

21           %end;

22         %mend;

23        

24         %square(act=y,y,2,3,4);

           _

           180

WARNING: Apparent invocation of macro SQUARE not resolved.

ERROR 180-322: Statement is not valid or it is used out of proper order.

Parmbuf (one f) never works for me.

%macro square(act=y) /parmbuff;

%if &act=y %then

  %do;

   %do i=1 %to %sysfunc(countw(&syspbuff));

    %let x=%scan(&syspbuff,&i);

          %if %sysfunc(anyalpha(&x))=0 %then %put The square of &x is %sysevalf(&x*&x);

   %end;

  %end;

%mend;

24         %square(2,3,4);

The square of 2 is 4

The square of 3 is 9

The square of 4 is 16

25         %square(2,3,4,act=y);

The square of 2 is 4

The square of 3 is 9

The square of 4 is 16

I see same as Tom.

SAS9.2 on linux.

Super User
Super User
Posts: 6,502

Re: difference between parameter macro and positional macro

You are not getting error messages for the later %MACRO statements because SAS is ignoring everything until it gets the %MEND for first one it rejected.

Trusted Advisor
Posts: 1,300

difference between parameter macro and positional macro

These were all ran in completely separate sessions for me...

Super User
Super User
Posts: 6,502

difference between parameter macro and positional macro

And the runs you posted DID get the error message.

Ask a Question
Discussion stats
  • 22 replies
  • 19365 views
  • 0 likes
  • 6 in conversation