Pages

SyntaxHighlighter

Thursday, May 24, 2012

Enhanced Editor Preferences

Here are some suggested Enhanced Editor settings and related best practices. Select Tools | Options | Enhanced Editor... and enabled Show line numbers, Insert spaces for tabs and Replace spaces with tabs on file open. I also like to set my tab size to 2 spaces (see below).

The other suggested setting is achieved by selecting Tools | Options | Preferences... and setting Recently used file list to the maximum value of 30 entries.

With those settings in place here are some suggested best practices when writing code:

  • One line of code (ends in semicolon) per line making it easier to read and maintain
  • Indent each subservient line with 2 spaces
  • Start each SQL column with a comma so it is self contained and easy to comment out
  • Align columns as appropriate to make it easier to read and maintian

Example

    proc sql noprint stimer ;
      connect to odbc ( "%getconnection( server = Prod )" ) ;
        create table commodity as
          select   *
            from   connection to odbc
            (
              select        commoditycode
                      /*  , commodityabbreviation
                          , commodityname   */
                          , count(*) as count
                from        arm.common.commodity       
                where       commoditycode = '0041'
                group by    commoditycode
                      /*  , commodityabbreviation
                          , commodityname   */
                order by    commoditycode
                      /*  , commodityabbreviation
                          , commodityname   */
            ) ;
      disconnect from odbc ;
    quit ;
Compare and contrast the ease of using leading commas versus a conditional trailing comma in this macro based example:
    %do i=1 %to 10;
      , yr&i._acre_qty as acre&i 
      , yr&i._yield_qty as yield&i
    %end;
or
    %do i=1 %to 10;
      %if &i<10 %then %do;
        yr&i._acre_qty as acre&i ,
        yr&i._yield_qty as yield&i ,
      %end;
      %else %do;
        yr&i._acre_qty as acre&i ,
        yr&i._yield_qty as yield&i 
      %end;
    %end;

Tuesday, May 22, 2012

Enhanced Editor Shortcut Keys

The SAS Enhanced Editor contains many shortcut keys that can improve productivity. See the below table for a list of some of the more powerful, yet often unknown commands and their related keys.

Shortcut Keys
Bookmark (toggle) line Ctrl + F2
Comment selection with comments Ctrl + /
Context Menu Shift + F10
Convert selected to lowercase Ctrl + Shift + L
Converted selected to uppercase Ctrl + Shift + U
Go to next marked line F2
Go to previous marked line Shift + F2
Indent selected text TAB
Move to matching brace/paren Ctrl + [
Move to matching DO/END keyword Alt + [
Rectangular selection Alt + left mouse button (LMB)
Remove trailing blanks (white spaces) Ctrl + Shift + W
Select all Ctrl + A
Uncomment selection with line comments Ctrl + Shift + /
Unindent selection Shift + Tab

Examples

Bookmarking - select the desired line and use the Ctrl + F2 keys to bookmark the line (a cyan image will appear to left of the code). Now if you scroll anywhere else in the file you can quickly return to this bookmark by using the F2 key while Shift + F2 takes you to the previous mark if you have multiple bookmarks. Press Ctrl + F2 again to remove a bookmark.

Rectangular Selection - Use the combination of the Alt + LMB to define a rectangular selection. Notice in the below example that we are able to select the table prefix and easily remove that reference using this technique.

Reference: Doing More with the SAS Display Manager: From Editor to View Table - Options and Tools You Should Know by Art Carpenter.

Friday, May 18, 2012

Macro Quoted String > 262 characters

If you have a SAS macro variable that is longer than 262 characters in length you will get a WARNING message such as the one shown below:

WARNING 32-169: The quoted string currently being processed has become more than 262 characters long. You may have unbalanced quotation marks.

The way to get around this limitation is to use the option named NOQUOTELENMAX as in

OPTIONS NOQUOTELENMAX ;