Hi,
I was wondering if there is a way to retrieve the default value of a macro parameter in a macro variable or so.
%macro demo (lib=work);
%if &lib.= %str( ) %then
%do;
%let &lib. = <default macro parameter value>;
%end;
%mend demo;
%demo(lib=);
Hi @xxformat_com,
I think with your %IF-%THEN logic you intend to override the default value functionality of the %MACRO statement and hence you don't need the latter, as Tom has pointed out already.
If instead you did want to distinguish between the two cases "parameter not specified" and "parameter empty/blank," you could assign the default value to a (local) macro variable to avoid typing the value twice:
%macro test(lib=&default);
%local default;
%let default=work;
%put Default parameter value: &default;
%put &=lib;
%mend test;
%test(lib=)
%test()
%test;
Can't you just test if the value is null, set it to WORK (which is the default)??
%if &lib.= %str( ) %then %let lib=work;
If that's not what you want, explain the reason for doing this (I'm asking for reasons, not code).
Well, it means that the information is maintained in multiple places. If the value is changed in one place, it means that you have to make sure that it is updated everywhere. It requires extra attention to the developer, to the person validation the program and to any one taking over the program. It is just prone to unecessary human error.
Just copy/paste from three lines above. After all, you wrote it into the definition, so you know what to use later in the macro.
@xxformat_com wrote:
Hi,
I was wondering if there is a way to retrieve the default value of a macro parameter in a macro variable or so.
%macro demo (lib=work); %if &lib.= %str( ) %then %do; %let &lib. = <default macro parameter value>; %end; %mend demo; %demo(lib=);
Just put the default into one place. Move it from the %MACRO statement into your %IF/%THEN logic.
%macro demo (lib=);
%if 0=%length(&lib) %then %let lib=WORK ;
%put &=lib ;
%mend demo;
%demo(lib=);
Also it is much easier to test for empty values by actually testing if the value is empty. By using %LENGTH(). If someone goes to the work of using macro quoting so they could pass in a space character that is not an empty value.
Hi @xxformat_com,
I think with your %IF-%THEN logic you intend to override the default value functionality of the %MACRO statement and hence you don't need the latter, as Tom has pointed out already.
If instead you did want to distinguish between the two cases "parameter not specified" and "parameter empty/blank," you could assign the default value to a (local) macro variable to avoid typing the value twice:
%macro test(lib=&default);
%local default;
%let default=work;
%put Default parameter value: &default;
%put &=lib;
%mend test;
%test(lib=)
%test()
%test;
That sounds like even more work.
Personally I just use a macro to validate macro parameters that includes a option to specify the default value.
filename parmv url "https://raw.githubusercontent.com/sasutils/macros/master/parmv.sas";
%inc parmv;
%macro test(lib);
%parmv(lib,_def=work)
%put &=lib ;
%mend test;
Example calls:
2556 %test; LIB=WORK 2557 %test(sasuser) LIB=SASUSER 2558 %test(lib=sasuser) LIB=SASUSER 2559 %test(lib=a b c) ERROR: TEST user error. ERROR: A B C is not a valid value for the LIB parameter. ERROR: The LIB parameter may not have multiple values. LIB=A B C
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.