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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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)

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26

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

 

--
Paige Miller
PeterClemmensen
Tourmaline | Level 20

Works fine for me? You are missing a semicolon after your %end statement though?

PaigeMiller
Diamond | Level 26

@PeterClemmensen wrote:

You are missing a semicolon after your %end statement though?


Yes, I caught that too, but forgot to mention it Smiley Frustrated

--
Paige Miller
Tom
Super User Tom
Super User

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: Call for Content

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!

Submit your idea!

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
  • 4 replies
  • 910 views
  • 0 likes
  • 4 in conversation