BookmarkSubscribeRSS Feed
Ashwini
Calcite | Level 5

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

Thanks,

Regards,

Ashwini

22 REPLIES 22
art297
Opal | Level 21

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.

Tom
Super User Tom
Super User

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. 

Ksharp
Super User

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

Tom
Super User Tom
Super User

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) ;

Ksharp
Super User

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

Peter_C
Rhodochrosite | Level 12

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

art297
Opal | Level 21

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;

Tom
Super User Tom
Super User

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)

Peter_C
Rhodochrosite | Level 12

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")

art297
Opal | Level 21

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

FriedEgg
SAS Employee

%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.

Tom
Super User Tom
Super User

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.

FriedEgg
SAS Employee

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

Tom
Super User Tom
Super User

And the runs you posted DID get the error message.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 22 replies
  • 34890 views
  • 0 likes
  • 6 in conversation