Hi,
I have a test macro, as shown:
%macro test (rule = "");
%if &rule. = "" %then %do;
Data Example;
set Source;
run;
%end
%MEND test;
I need to have rule set to ¨¨ as its default value, but if I give an argument to the macro, I want it to use that value.
But if i try %test(); or %test; or even %test("value"); they don't run. Is it possible to have default values for arguments?
Thanks
If you define the parameter as named (which is required to take advantage of the default value) then you need to call it with the name.
So your last example call would not work.
Examples of call that would work with your macro definition.
%test;
%test()
%test(rule=some other value)
Otherwise add your own code for default values into the body of the macro.
%macro test(rule);
%if %length(&rule)=0 %then %let rule=Default value;
....
%mend test;
Then you can pass the value of RULE by position. Note that you can still call it with the name if you want.
%test;
%test()
%test(some other value)
%test(rule=some other value)
Seems to execute for me. By the way, you ought not use quotes or double-quotes to enclose the values of your macro variables.
%macro test (rule = );
%put &=rule;
%if &rule. = %then %do;
Data Example;
set Source;
run;
%end;
%MEND test;
%test()
Works fine for me? You are missing a semicolon after your %end statement though?
@PeterClemmensen wrote:
You are missing a semicolon after your %end statement though?
Yes, I caught that too, but forgot to mention it
If you define the parameter as named (which is required to take advantage of the default value) then you need to call it with the name.
So your last example call would not work.
Examples of call that would work with your macro definition.
%test;
%test()
%test(rule=some other value)
Otherwise add your own code for default values into the body of the macro.
%macro test(rule);
%if %length(&rule)=0 %then %let rule=Default value;
....
%mend test;
Then you can pass the value of RULE by position. Note that you can still call it with the name if you want.
%test;
%test()
%test(some other value)
%test(rule=some other value)
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.