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

Thanks Tom for digging deeper into this. Here is the LOG :

NOTE: AUTOEXEC processing completed.

1    %let words=pretty awful;
2    %macro secondWord(w=%sysfunc(dequote("%scan(&words,2)")));
3      %put %superq(w);
4      %let words=pretty bad;
5      %put &w;
6    %mend secondWord;
7    %let words=much better;
8    %secondWord;
%sysfunc(dequote("%scan(&words,2)"))
bad

Seems like nothing is ever processed in the macro parameter default values.

PG


PG
Astounding
PROC Star

OK, third approach.

Why have a parameter at all?  Just keep the creation of &LIB out of the macro entirely.

If this %MyMacro is part of a library of macros, you can store more than the definition of the macro.  Within mymacro.sas, store the definition of the macro, WITHOUT a LIB parameter.  Also in the same mymacro.sas file but outside of the macro definition, store a DATA step (or other code) that assigns the proper value to &LIB and makes it a global variable.

Whether you subsequently define %MyMacro using %INCLUDE or via an autocall invocation, the value of &LIB will get set at that time but will not get reset later.

If you take this approach, you are probably better served by changing the name of the macro variable.  &LIB is common enough that it might conflict with other macros.

Good luck.

PGStats
Opal | Level 21

Thanks Astounding for persevering! What I finally did in light of this discussion is something like :

%MACRO myMacro(LIB=%scan(&SYSDSN,1));

%let _lib=&LIB;

%let _SYSDSN=&SYSDSN;

...

%let SYSDSN=&_SYSDSN;

%mend;

and I use _lib inside the macro. This way I get parameter binding at invocation time and I don't change the name of the last dataset with some intermediate dataset created inside the macro. Seems to work.

PG

PG
Astounding
PROC Star

Well, color me Astounded.   :smileyshocked:    I had no idea you could assign a value to an automatic macro variable.  I guess some you can, some you can't.  All in all, an interesting problem.

Ksharp
Super User

PG.

I recommend you not to use underline as the first character of Macro variable.

Under some OS, it would be broken.

Ksharp

Astounding
PROC Star

Again, depends on what is really needed, but you might try defining the original macro using the NAME, rather than the VALUE of a macro variable.  For example, at the right spot outside of every macro definition:

%let dataset_to_use = %scan(&sysdsn,1);

Then the macro definition begins with:

%macro MyMacro (LIB=dataset_to_use);

Then inside the macro, reference &&&lib

Tom
Super User Tom
Super User

Here is the response from SAS Technical support about why 9.3 is different.

Apparently it has been broken for the last 30 years!

Starting in SAS V6.08 and continuing through SAS V9.2, the SAS Macro Facility was erroneously fully evaluating expressions at macro compilation time which defined the default value of keyword parameters.  This was a change from SAS V5.18.  At macro compilation time, the SAS Macro Facility should have only been evaluating %STR and %NRSTR quoting functions used in the default value expressions of keyword parameters.  The correct time for full resolution of default keyword parameter expressions is at macro execution time.  The change seen in SAS V9.3 is due to the correction to SAS Macro Facility to match the correct behavior of SAS V5.18.

Thank You,

Russ Tyndall

SAS Technical Support Analyst

SAS Certified Advanced Programmer for SAS 9

PGStats
Opal | Level 21

Thank you, Tom!

I wonder how much code was written since V5.18 that rely on that erroneous behaviour! I think I like the fixed behaviour (very late binding) better than the erroneous behaviour (very early binding) but what I was naively expecting was binding at invocation time, since invocation is when parameters are normally bound to their values. But that was too simple.Smiley Happy

PG

PG

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
  • 1451 views
  • 7 likes
  • 6 in conversation