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

I have written the pseudo program below to demonstrate what I want to ask (I am writing a more complex function in reality).

My macro function has 2 parameters, and as seen in the program I do an Nested %IF. 

Question 1: If I understand it correctly, Nested %IF (i.e. a Macro Nested IF) is only allowed inside a "Macro Wrapper" (and not in "open code"), i.e. it should be between %Macro and %Mend. 

Question 2: In the first %Else %Do in the program below, how would I tell SAS to do nothing, i.e. just continue? I am not entirely sure wether this makes sense either. 
Also, would it not be weird to exclude the code: 

		%else %do; 
			Nothing.   * How do you tell SAS to do nothing, i.e. just continue?; 
		%end; 

Since you then do not exhaust all the possible alternatives? 

Question 3: I could in my macro write a help text (some kind of documentation) but it is quite uggly that the user must open the macro to read the help text. Any best practices for this? 

Question 3.1. Is it possible to add a help text of the kind shown with the picture: 

SasStatistics_1-1624374270773.png

 

 

 

 

%Macro MyMacro(VariableA, VariableB); 
/* Here I could write a help text to the user, but it is quite uggly that the user must open the function to get a feeling for it */
%if &VariableA. = Yes %then %do; %if &VariableB. = B1 %then %do; Run some Code %end; %else %if &VariableB = B2 %then %do; Run some Code %end; %else %do; Nothing. * How do you tell SAS to do nothing, i.e. just continue?; %end; %end; %else %do; %put Note: VariableA should be Yes to run the program. ; %end; %mend MyMacro;

 

 

Thanks. 

1 ACCEPTED SOLUTION

Accepted Solutions
Amir
PROC Star

Hi @SasStatistics,

 

In response to your questions:

 

Question 1: If I understand it correctly, Nested %IF (i.e. a Macro Nested IF) is only allowed inside a "Macro Wrapper" (and not in "open code"), i.e. it should be between %Macro and %Mend. 

Answer 1: Correct.

 

 

Question 2: In the first %Else %Do in the program below, how would I tell SAS to do nothing, i.e. just continue? I am not entirely sure wether this makes sense either. 
Also, would it not be weird to exclude the code: 

Answer 2: If you only want action to be taken when %if conditions are satisfied, then there is no need to have %else-%do-%end as no action is required.

 

 

Question 3: I could in my macro write a help text (some kind of documentation) but it is quite uggly that the user must open the macro to read the help text. Any best practices for this? 

Answer 3: I would suggest that this is subjective, someone else might see this this as due diligence or attention to detail. I'm not aware of any best practice other than to document your code, which implies someone will be reading the comments.

 

 

Question 3.1. Is it possible to add a help text of the kind shown with the picture: 

Answer 3.1: If you really don't want the macro definition to be read, then one idea (that works in EG 8.2) is to make the first argument to the macro be a description and provide an inline comment, then when you type the macro name in the program editor followed by a '(' then the description will appear as in the below screen shot. You can just enter a comma (e.g., %ds_info(,sashelp,class)) to move on to the next argument.

 

Amir_1-1624401575351.png

 

 

HTH

 

 

Thanks & kind regards,

Amir.

View solution in original post

14 REPLIES 14
PaigeMiller
Diamond | Level 26
%else %do; 
/* No command here */
%end; 

This works, but is unnecessary. The logic of your code indicates that if you leave out this %else %do portion, that's what you should do, and then nothing happens if you don't meet any of the above conditions. This has nothing to do with macros specifically, it is computer logic.

 

%Macro MyMacro(VariableA, VariableB);  
    %if &VariableA. = Yes %then %do; 
        %if &VariableB. = B1 %then %do; 
			Run some Code
        %end;
        %else %if &VariableB = B2 %then %do;
			Run some Code 
        %end; 
    %end; 
    %else %do; 
        %put Note: VariableA should be Yes to run the program. ; 
    %end;
%mend MyMacro; 

Again, why bother calling the macro arguments VariableA and VariableB? Using Variable as part of a variable name adds nothing beneficial, but it is harder to type and offers opportunities for error. I would name these variables A and B.

--
Paige Miller
SasStatistics
Pyrite | Level 9
Thanks Paige. Used VariableA and VariableB in this demo program for clarity.

Ps. Must say I am suprised you did not have an answer to all the questions in the post as you usually do.. 😉
PaigeMiller
Diamond | Level 26

Used VariableA and VariableB in this demo program for clarity.

What else can they be but variables? If you name them A and B, everyone knows they are not giraffes nor are they restaurants ... they have to be variables, they can't be anything else.

--
Paige Miller
Tom
Super User Tom
Super User

@PaigeMiller wrote:

Used VariableA and VariableB in this demo program for clarity.

What else can they be but variables? If you name them A and B, everyone knows they are not giraffes nor are they restaurants ... they have to be variables, they can't be anything else.


The name VARIABLEA makes sense if the value of the parameter is the name of the variable to be used in the SAS code that the macro generates.  Otherwise users might use a value instead of name when calling the macro.

SasStatistics
Pyrite | Level 9
Haha true!
Tom
Super User Tom
Super User

Personally I code the %MACRO statement in a form that serves as the documentation.  For example see https://github.com/sasutils/macros/blob/master/direxist.sas

%macro direxist
/*----------------------------------------------------------------------
Test if directory exists
----------------------------------------------------------------------*/
(path    /* Name of directory to test for existance */
);

With display manager I even have command macro that searches for the file in the autocall library a pulls the %MACRO statement from the file and converts into a macro call and pastes into into the program editor.  If you are using Enterprise Guide or SAS/Studio you will have look for an alternative method for doing that.

 

You can code the macro to include a help option.

%macro mymacro(....,help=0);
%if "&help" ne "0" %then %do;
  %put To use %MYMACRO ....;
  %put ....;
  %return;
%end;
....
%mend;

Then users can ask for help and it will print in the log.

%mymacro(help=Yes);

 

SasStatistics
Pyrite | Level 9

Thanks @Tom . In the second alternative with the help option, what is the purpose of the %return statement?

Reading the documentation: https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/mcrolref/p0qyygqt5a69xnn1rhfju3kjs8al.htm

My guess is that the purpose is that if the user wants to see the help, the "total macro" is not run, only the help text is displayed?

Reeza
Super User

You leave it blank.

In fact, you don't even need the %ELSE/%DO.

%else %do;

%end;



There is also LEAVE/CONTINUE.
https://blogs.sas.com/content/iml/2017/03/15/leave-continue-sas.html

ballardw
Super User

To do nothing do not put anything between the %do; %end; Though in your example the %else is not needed, an if the purpose is not to do anything seldom would be unless you want a place holder for later code insertion.

Especially do not place a non-macro language comment.

Your Example macro has unbalanced %if %then %do as written. You have an %end without a %do. So your "nesting is incorrect as is.

 

 

%else %do;
%end;

 

 

If you want to place a comment in a macro you have to use the /*  comment text*/ or %*comment text; style of comments. Otherwise a comment like *comment text; is generated code and can cause some entertaining issues depending on the content.

 

 

Amir
PROC Star

@ballardw, I too initially thought there was an extra %end, but the matching %do can be found on the very first line, after scrolling to the far right of the screen. Some formatting issues at play.

 

Kind regards,

Amir.

Amir
PROC Star

Hi @SasStatistics,

 

In response to your questions:

 

Question 1: If I understand it correctly, Nested %IF (i.e. a Macro Nested IF) is only allowed inside a "Macro Wrapper" (and not in "open code"), i.e. it should be between %Macro and %Mend. 

Answer 1: Correct.

 

 

Question 2: In the first %Else %Do in the program below, how would I tell SAS to do nothing, i.e. just continue? I am not entirely sure wether this makes sense either. 
Also, would it not be weird to exclude the code: 

Answer 2: If you only want action to be taken when %if conditions are satisfied, then there is no need to have %else-%do-%end as no action is required.

 

 

Question 3: I could in my macro write a help text (some kind of documentation) but it is quite uggly that the user must open the macro to read the help text. Any best practices for this? 

Answer 3: I would suggest that this is subjective, someone else might see this this as due diligence or attention to detail. I'm not aware of any best practice other than to document your code, which implies someone will be reading the comments.

 

 

Question 3.1. Is it possible to add a help text of the kind shown with the picture: 

Answer 3.1: If you really don't want the macro definition to be read, then one idea (that works in EG 8.2) is to make the first argument to the macro be a description and provide an inline comment, then when you type the macro name in the program editor followed by a '(' then the description will appear as in the below screen shot. You can just enter a comma (e.g., %ds_info(,sashelp,class)) to move on to the next argument.

 

Amir_1-1624401575351.png

 

 

HTH

 

 

Thanks & kind regards,

Amir.

andreas_lds
Jade | Level 19

@Amir unfortunately the description is only displayed if the macro is called in the same file it is defined. The description won't be shown if the macro is loaded by autocall/sasautos.

 

Question 1 again: Nesting also occurs if you use %include in the %then (%else) part of the if statement and the included program uses an %if statement.

Amir
PROC Star

@andreas_lds, good to know; thanks for sharing.

ChrisNZ
Tourmaline | Level 20

Q1 My version of SAS is too old to know anything about open code %if

Q2 You can do that but it serves no purpose.

  It sometimes is clearer to write an empty then: %if ...%then; %else ...; as the opposite test is more cumbersome.

  That's rare though.

Q3 See how I do it in the attached file. Not best practice, just my method. Similar to @Tom 's method except I don't have a help parameter. If the first parameter is missing or HELP then I call the help screen.

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 14 replies
  • 1144 views
  • 7 likes
  • 8 in conversation