BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
xxformat_com
Barite | Level 11

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

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;

View solution in original post

6 REPLIES 6
PaigeMiller
Diamond | Level 26

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

--
Paige Miller
xxformat_com
Barite | Level 11

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.

Tom
Super User Tom
Super User

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

FreelanceReinh
Jade | Level 19

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;
Tom
Super User Tom
Super User

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

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 6 replies
  • 1286 views
  • 2 likes
  • 5 in conversation