Amadeus cookies policy - you'll see this message only once.

Amadeus use cookies on this website. They help us to know a little bit about you and how you use our website, which improves the browsing experience and marketing - both for you and for others. They are stored locally on your computer or mobile device. To accept cookies, continue browsing as normal. Or, go to the privacy policy for more information.

Conditionals in Macros

Here is a line of code from a macro definition. It is supposed to be issuing a Unix command to delete a file. Spot the deliberate mistake!

%if (&a = &b) %then %sysexec rm oldfile.txt;

Yes, we have left off a semicolon. We needed to say:

%if (&a = &b) %then %sysexec rm oldfile.txt;;

Intuitive, isn't it? One semicolon is needed to terminate the %if/%then construction, and then a second to terminate the %sysexec command. With only one semicolon, it is the %sysexec command that remains unterminated, and so our next line of code is going to be interpreted as the names of more files to be deleted. (Better hope it doesn't contain an asterisk anywhere!)

Moral: always use %do and %end, even if you're only generating one line (or less) e.g.

%if (&a = &b) %then
   %do;
    %sysexec rm oldfile.txt;
  %end;

Incidentally, if a and b are numeric strings it would be better to write the test as:

%if %sysevalf(&a = &b)

The macro processor will regard a and b as character strings, and so would, for example, regard "3" as different from "3." and "4.7" as different from "4.700". Using an evaluation macro will prevent such potentially confusing results.