Pages

SyntaxHighlighter

Wednesday, December 19, 2018

Macro %then semicolon

As a creature of habit, I typically write %IF %THEN/%ELSE statements using %DO blocks. However, is a %DO block needed for a one line statement? This blog post answers that question via a series of test conditions. Please understand that this is a very simple example program to illustrate the problem that was experienced.

Test 1 single semicolons

%macro test();
   data mydsn;
      %if &sysver = 9.4 %then x = 1;
      %else x = 0;
      date = date();
   run;
%mend;
%test();

80 date = date();   run;
                    ____
                    22
 
ERROR 22-322: Syntax error, expecting one of the following: !, !!, &, *, **, +, -, /, ;, <, <=, <>, =, >, ><, >=, AND, EQ, GE, GT, IN, LE, LT, MAX, MIN, NE, NG, NL, NOTIN, OR, ^=, |, ||, ~=.  

That seems odd, but I guess I need to add another semicolon to each of the %IF and %ELSE lines to terminate the macro language statements. Here are the results of that attempt.

Test 2 - using two semicolons

 73         %macro test();
 74            data mydsn;
 75               %if &sysver = 9.4 %then x = 1;;  /* notice use of two semicolons */
 76               %else x = 0;;                    /* notice use of two semicolons */
ERROR: There is no matching %IF statement for the %ELSE.
ERROR: A dummy macro will be compiled.
 77               date = date();
 78            run;
 79         %mend;
 80         %test();

Test 3 - two semicolons on the %ELSE line

%macro test();
   data mydsn;
      %if &sysver = 9.4 %then x = 1;  /* one semicolon here */
      %else x = 0;;                   /* two semicolons here */
      date = date();
   run;
%mend;
%test();

NOTE: The data set WORK.MYDSN has 1 observations and 2 variables.

While the use of one semicolon on the first line and two on the second macro language line works, I find it confusing. In the name of ease of reading and maintaining, I still prefer the use of %DO blocks.

Test 4 - %DO blocks

%macro test();
   data mydsn;
      %if &sysver = 9.4 %then %do;
       x = 1;
      %end;
      %else %do;
       x = 0;
      %end;
      date = date();
   run;
%mend;
%test();

NOTE: The data set WORK.MYDSN has 1 observations and 2 variables.

1 comment:

  1. Invest with 200$ and get a returns of 5,000$ within seven business working days.Why wasting your precious time online looking for a loan? When there is an opportunity for you to invest with 200$ and get a returns of 5,000$ within seven business working days. Contact us now for more information if interested on how you can earn big with just little amount. This is all about investing into Crude Oil and Gas Business.

    Email: investmoneyoilgasfast@gmail.com

    ReplyDelete