Errors that cause SAS to "freeze"… and what to do about them

August 19, 2013
By

This post was kindly contributed by The DO Loop - go there to comment and to read the full post.

Even the best programmers make mistakes. For most errors, SAS software displays the nature and location of the error, returns control to the programmer, and awaits further instructions.
However, there are a handful of insidious errors that cause SAS to think that a statement or program is not finished. For these errors, SAS doesn’t display the error because it is waiting for the programmer to finish submitting the rest of the statement. Meanwhile, the programmer (who is unaware that an error has occurred) is waiting for SAS to respond. From the programmer’s point of view, SAS is frozen. It has gone off into La-La Land, or maybe the Twilight Zone.

Fortunately, there is a simple “magic command” that fixes them all of these common errors. The common errors that render SAS unresponsive are as follows:

  • The forgotten semicolon: If the last statement in a program does not contain a terminating semicolon, SAS thinks that the program is not finished. It waits to receive the rest of the statement. Without a terminating semicolon, SAS will wait, and wait, and wait….
    y = 1        /* No semicolon, so statement not complete */
  • The forgotten closing single quote: If your program starts a string but forgets to end it, SAS thinks you are in the process of defining a string. You can submit statements such as QUIT and ENDSAS, but SAS thinks these statements are just part of the string and does not execute them.

    c = 'My string;     /* No closing quote. Future stmts are part of string */
    run;                * Hey! SAS is frozen! ;
    endsas;             * Argh! Nothing works! ;

    As shown above, you can detect this error visually if you are using a program editor in which syntax is color-coded. For example, in the SAS enhanced editor, all characters after the equal sign are colored purple, which indicates that SAS thinks they are all part of a string. Also, after the character string exceeds 256 characters, SAS writes a helpful warning to the SAS Log:

    WARNING: The quoted string currently being processed has become
             more than 262 characters long.  You might have
             unbalanced quotation marks.
  • The forgotten closing double quote: Same issue as for the forgotten single quote.
  • The forgotten closing comment: You started a comment, but haven’t closed it with */. No matter what text you submit, SAS thinks it is part of the comment.
    c = 'My string';    /* Program is complete
    run;                * Hey! SAS is frozen! ;
    endsas;             * Argh! Nothing works! ;

    Again, if you use a color-coded program editor, you ought to be able to detect this error visually. In the SAS enhanced editor, you will notice that your statements are green.

There is a “magic command” that you can submit that will recover from all four errors:

;*';*";*/;

If you have used SAS Enterprise Guide, you’ve probably seen this special statement (also called the “magic string” or the “quote killer”) appended to the end of submitted programs. It is used by many client applications to ensure that the SAS server terminates and produces results such as ODS tables and graphics. I don’t know who originally invented the magic command, but let’s look at what it does:

  • If the submitted program is already properly terminated (none of the errors are present), the command issues a null statement (the first character) and a comment (the remaining characters).
  • If the submitted program forgot a concluding semicolon, the command terminates the previous statement (the first character) and issues a comment (the remaining characters).
  • If the submitted program forgot to close a single-quote string, the command terminates the string (the third character) and issues a comment (the remaining characters).
  • If the submitted program forgot to close a double-quote string, the command terminates the string (the sixth character) and issues a comment (the remaining characters).
  • If the submitted program is missing a closing comment symbol, the command closes the comment (the eighth and ninth characters) and issues a null statement (the last character).

In all cases, the magic command causes SAS to escape from La-La Land and returns control to the programmer.

A forgotten RUN or QUIT statement is another error that can cause SAS to be unresponsive. For most procedures, SAS parses the statements in a program, but does not execute them until it encounters a RUN or QUIT statement. (Exceptions include some interactive procedures such as the IML and SQL procedures.)
This kind of programming error is obviously fixed by submitting a QUIT or RUN statement. (Some programmers use the RUN CANCEL statement to abort a submitted DATA step.) Consequently, some programmers might want to modify the magic string as follows:

;*';*";*/;quit;

Again, this version of the magic command is used by many SAS client applications, including EG. It looks mysterious the first time you see it, but after you dissect it, it makes perfect sense. If you have ever asked “what is the purpose of the statement at the end of SAS Enterprise Guide programs,” now you know!

Do you have a debugging tip that you use to overcome an insidious error? What do you do to regain control when your SAS program contains an error that locks-up your computer? Leave a comment.

tags: Getting Started, SAS Programming

This post was kindly contributed by The DO Loop - go there to comment and to read the full post.

Tags: , ,

Welcome!

SAS-X.com offers news and tutorials about the various SAS® software packages, contributed by bloggers. You are welcome to subscribe to e-mail updates, or add your SAS-blog to the site.

Sponsors







Dear readers, proc-x is looking for sponsors who would be willing to support the site in exchange for banner ads in the right sidebar of the site. If you are interested, please e-mail me at: tal.galili@gmail.com
SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. ® indicates USA registration.