Вы находитесь на странице: 1из 15

NESUG 2010

Foundations and Fundamentals

SAS® Enhanced Editor: Efficient Techniques Kathy Harkins, Merck & Co., Upper Gwynedd, PA

ABSTRACT

Whether you are a novice or experienced SAS programmer, this paper will highlight time saving functionality available in the SAS® Enhanced Editor. The focus of this paper is to provide an overview of keyboard functions and commands for efficient program creation and review. Keyboard short-cuts and SAS Abbreviations® will be introduced as well as techniques for finding coding errors utilizing customized features of the Enhanced Editor.

INTRODUCTION

If you are like me, you probably go about your day to day SAS programming utilizing your favorite functions of the SAS Enhanced Editor. In general, our favorite functions cover only a subset of those available to us. To remedy this, I like to use the opportunity of informal discussions with colleagues to learn new tips and techniques of the editor. This paper will compile my collection of those informal tips as well as highlights from the SAS Enhanced Editor documentation for SAS V8 or later. The techniques will be organized into three main areas:

General Features and Functionality

Shortcuts and Abbreviations

Enhanced Editor Customization for Finding Coding Errors

By reading this paper you will enhance your knowledge of the SAS Enhanced Editor and learn new features to incorporate into your day to day SAS programming.

OVERVIEW

Following is a diagram and a description of the components of the SAS Enhanced Editor window.

Title Bar

Menu Bar

of the SAS Enhanced Editor window. Title Bar Menu Bar Command Bar Current Working Folder Cursor
of the SAS Enhanced Editor window. Title Bar Menu Bar Command Bar Current Working Folder Cursor
of the SAS Enhanced Editor window. Title Bar Menu Bar Command Bar Current Working Folder Cursor

Command Bar

SAS Enhanced Editor window. Title Bar Menu Bar Command Bar Current Working Folder Cursor Insertion Point

Current Working Folder

Cursor Insertion Point

Diagram 1: Overview of the SAS Enhanced Editor window

-

1 -

NESUG 2010

The components of the Enhanced Editor window are:

Menu Bar

Foundations and Fundamentals

The menu bar presents the menus available for the active SAS application window.

The menu bar will present different options as you navigate between application windows.

Title Bar

The name for the active SAS program. The default name for an untitled SAS program is Editor - Untitledx, where x is a number. By default, the first untitled program name will be Editor – Untitled1; if a second new SAS program is opened then its default name will be Editor – Untitled2, etc.

An * next to the file name in the Enhanced Editor title bar indicates that changes have been made to your program that have not yet been saved.

Command Bar

The command bar provides a way to quickly execute SAS commands. Type the command in the Command Bar and select enter to execute the command.

The command bar retains a list of the commands that you have already entered in a previous SAS session. Previously issued commands are automatically populated when you begin typing a command. Select the enter key to accept and the command will be executed, or just keep typing to override a previously issued command.

Current Working Folder

The current working folder is the default location where program files are opened from and saved to for the current SAS session.

You have the ability to change the working directory during your SAS session. To do so, issue the command DLGCDIR in the command bar. This will open the Change Folder window where you can navigate to the desired folder. This resets the current working folder for the duration of your SAS session.

Cursor Insertion Point

The cursor insertion point is the location, including line and column, where text will be entered in your editor window when you start typing. The default cursor insertion point is Line 1, Column 1.

GENERAL FEATURES AND FUNCTIONALITY

Navigating Program Code Following are helpful Enhanced Editor commands to assist in navigating through your program code.

Multiple Views:

It is possible to have multiple views open of the same file in the Enhanced Editor. This can be helpful when you want to view various sections of your SAS program simultaneously. Keep in mind that you are not

viewing multiple copies of your program; there is still only a single file open. When you open multiple views of a file, changes that you make in any view of the file are made simultaneously in all views. To open multiple views of the same file:

1.)

Make the desired file the active window

2.)

From the WINDOW Menu of your Enhanced Editor Window Menu Bar Choose New Window

Show Line Numbers The Enhanced Editor allows users the option to either display line numbers in the margin or to not display the line numbers in the margin. When line numbers are displayed, the current line number is red. The current line number is line 12 in the example below.

- 2 -

NESUG 2010

Foundations and Fundamentals

NESUG 2010 Foundations and Fundamentals In order to select line number preferences: From the Command Bar

In order to select line number preferences: From the Command Bar Type the word num or nums to toggle line numbers on and off.

Once line numbers are active, the "Go To Line Dialogue box" can be invoked to navigate to specific line numbers. To invoke the "Go To Line Dialogue Box" press the Ctrl key and the G key simultaneously. Enter the desired line number into the box and the cursor is taken to the specified line. The column position of the cursor on the specified line is dependent upon where the cursor was positioned when the "Go To" command was issued.

In the example below, the dialogue box is invoked and the number 29 is entered. The result is that line 29 becomes the active line (denoted by the red line number) and the cursor is moved to line 29.

by the red line number) and the cursor is moved to line 29. Showing Brackets and

Showing Brackets and Line Sections Optional section brackets in the margin and a section line across the editor window mark the beginning and end of sections code in your editor window.

- 3 -

NESUG 2010

Foundations and Fundamentals

NESUG 2010 Foundations and Fundamentals To enable or disable collapsible code sections, or brackets and lines:

To enable or disable collapsible code sections, or brackets and lines:

select Tools from the Menu Bar then Options then Enhanced Editor then General Tab and select the desired settings as shown in yellow below:

then Options then Enhanced Editor then General Tab and select the desired settings as shown in

- 4 -

NESUG 2010

Foundations and Fundamentals

Selecting Text In addition to highlighting full lines of text within the editor window by clicking from column 1 on a line through to the end of the line(s), you can more easily select a full line(s) of text by simply left-clicking the mouse in the margin area. This functionality is available regardless of whether line numbers are on or off. For example, suppose you wanted to select lines 20 to 27 in your editor window. Place your cursor in the margin area to the left of the number 20 and left-click the mouse and hold up until selecting down and including the number 27 in the margin area. The result being:

the number 27 in the margin area. The result being: Collapsible code sections Collapsible code sections

Collapsible code sections Collapsible code sections enable you to expand or collapse one section of code at a time in your editor window. A section begins on the signature line with a step keyword (DATA, PROC or %MACRO Statement),

a comment, or spaces above a section word or comment. A section ends with the next step keyword, a comment, or space above the next section word or comment.

An expanded section is indicated by a minus sign

a section, click the minus sign (

indicated by a minus sign a section, click the minus sign ( on the signature line

on the signature line in the margin. In order to collapse

( on the signature line in the margin. In order to collapse ). (Shown in red

). (Shown in red below).

the margin. In order to collapse ). (Shown in red below). A collapsed section is indicated

A collapsed section is indicated by a plus sign (

code that is displayed. To expand a section: click the plus sign (Shown in red below) or enter any keystroke on a signature line or paste into a signature line or type something above a section that affects the section,

such as comments or quotes.

that affects the section, such as comments or quotes. ) in the margin, and the signature

) in the margin, and the signature line is the only line of

- 5 -

NESUG 2010

Foundations and Fundamentals

NESUG 2010 Foundations and Fundamentals Note that selecting a line from the margin or starting above

Note that selecting a line from the margin or starting above the section that includes a collapsed segment actually includes all the collapsed text with the selection. However, selecting a collapsed line of text by dragging the mouse over the text will include only that single line of highlighted text.

SHORT-CUTS and ABBREVIATIONS

Built-In Keyboard Short-Cuts The Enhanced Editor has built-in keyboard functions that can prove to be very useful. The following key- board functions are detailed due to their efficiency and ease of use.

Comment and Un-Comment Sections of Code Commenting out one or more lines of code can be tedious and can also lead to keystroke mistakes that can cause your SAS code to not perform properly. There is a quick and simple way to comment out sections of highlighted program code:

To comment Code:

Select the lines of code to be commented and press the CTRL key and the / key simultaneously. The result being that /* and */ are placed around each line of highlighted text as shown below.

/*proc means*/

/*

data=LabSite01*/

/*

mean median range n min max ;*/

/*

var sen_glu;*/

/*

output out = means_any */

/*

(drop =_type_ rename = (_freq_ = count))*/

/*

mean=mean_val p50= median_val range=range_val min=min_val

max=max_val;*/

/*run;*/

To Un-comment Code:

Select the lines of code to be commented and press the CTRL key and the SHIFT key and the / key simultaneously. The selected lines of code are then un-commented as shown below.

proc means

data=LabSite01

mean median range n min max var sen_glu; output out = means_any (drop =_type_ rename = (_freq_ = count)) mean=mean_val p50= median_val range=range_val min=min_val max=max_val; run;

;

- 6 -

NESUG 2010

Foundations and Fundamentals

Converting the Case of Text There are two simple keyboard short-cuts available for those instances where the upper or lower case is needed.

To convert text to Lower Case:

Select the lines of code to be converted and press the CTRL key and the SHIFT key and the L key simultaneously.

proc import out= LabSite01 datafile= "\\ushome06\harkins\PROJECTS\LabSite01.xls" dbms=EXCEL replace; getnames=yes; mixed = yes;

run;

To convert text to Upper Case:

Select the lines of code to be commented and press the CTRL key and the SHIFT key and the U key simultaneously.

PROC IMPORT OUT= LABSITE01 DATAFILE= "\\USHOME06\HARKINS\PROJECTS\LABSITE01.XLS" DBMS=EXCEL REPLACE; GETNAMES=YES; MIXED = YES;

RUN;

Finding Matching Keywords Simple key strokes are available to find the matching brace, parenthesis or DO/END keywords while reviewing SAS code.

To find matching brace ] or parenthesis ):

If you place the cursor in front of a brace or parenthesis and issue the CTRL key and the [ key simultaneously, then the cursor will move to the matching ] or ).

(drop =_type_ rename = (_freq_ = count))

matching ] or ). (drop =_type_ rename = (_freq_ = count)) Position the cursor here Press

Position the cursor here

Press the CTRL key and the [ key simultaneously

And the cursor will jump to the matching ) as below.

(drop =_type_ rename = (_freq_ = count))

) as below. (drop =_type_ rename = (_freq_ = count)) To find matching DO – END

To find matching DO – END Pairs:

If you place the cursor within a DO-END block and press the ALT key and the [ key simultaneously, then the cursor moves first to the DO keyword within the block.

- 7 -

NESUG 2010

For example:

Position the cursor here

data process; set LabSite01; if sen_glu > . then do; temp = last_name; last_name = first_name; first_name = temp;

end;

= last_name; last_name = first_name; first_name = temp; end; Foundations and Fundamentals Pressing the ALT key

Foundations and Fundamentals

Pressing the ALT key and the [ key simultaneously will position the cursor at the DO keyword shown below.

data process; set LabSite01; if sen_glu > .

then do;

. data process; set LabSite01; if sen_glu > . then do; temp = last_name; last_name =

temp = last_name; last_name = first_name; first_name = temp;

end;

Pressing the ALT key and the [ key simultaneously again will position the cursor at the END keyword shown below.

data process; set LabSite01; if sen_glu > .

then do;

temp = last_name; last_name = first_name; first_name = temp;

end;

= last_name; last_name = first_name; first_name = temp; end; Note that if there no DO or

Note that if there no DO or END keywords found when pressing the ALT key and the [ key simultaneously then the cursor remains where it was initially.

User Defined Keyboard Short-Cuts The Enhanced Editor provided the flexibility for the user to define his or her own keyboard shortcut keys. Three examples will be provided. The first example is to create a shortcut key that clears the log window and the output window simultaneously. To access short-cut keys:

Issue the keys command in the Command Bar or from the Menu Bar Select Tools Then Options Then Keys

Either method will open the KEYS window in your PC SAS session. A portion of the window is shown below:

- 8 -

NESUG 2010

Foundations and Fundamentals

NESUG 2010 Foundations and Fundamentals Enter the text clear output; clear log next the F12 line

Enter the text clear output; clear log next the F12 line as shown in yellow below.

clear log next the F12 line as shown in yellow below. Next, close your KEYS window.

Next, close your KEYS window. You have now associated both the commands to clear the output and to clear the log window with the function key F12. Pressing the F12 will now clear both the log and output windows simultaneously. Once you have defined a key short-cut it is available for all future SAS sessions.

The second example of a user-defined shortcut key is to collapse all folding blocks and a third example will be to expand all folding blocks. To create the key to collapse all folding blocks it is necessary to use the following Menu Bar sequence:

from the Menu Bar Select Tools Then Options Then Enhanced Editor Keys

Which will open the following dialogue box:

- 9 -

NESUG 2010

NESUG 2010 Foundations and Fundamentals Next Select "Assign Keys" on the right-hand side as shown above.

Foundations and Fundamentals

Next Select "Assign Keys" on the right-hand side as shown above. Next, Select "Collapse all Folding Blocks" from the Commands list on the left hand-side.

Blocks" from the C ommands list on the left hand-side. Place your Cursor in "Press new

Place your Cursor in "Press new shortcut key" box on the lower right

And press the F11 key. This causes the F11 key to be entered in the Press new shortcut key box. You can not type the letters F and then 11. You must press the F11 key itself. See the result below:

- 10 -

NESUG 2010

NESUG 2010 Click the "Assign" box to active the shortcut key. Foundations and Fundamentals Now when

Click the "Assign" box to active the shortcut key.

Foundations and Fundamentals

Now when pressing the F11 Key, all collapsable code sections are closed as shown below:

all collapsable code sections are closed as shown below: For the third example of user-defined Keys,

For the third example of user-defined Keys, we will define the F10 key to be the keyboard shortcut to expand all collapsed code sections. The necessary sequence of steps is identical to those from example 2 above, except choose Expand all folding blocks and choose F10 in the Assign Keys window as below:

- 11 -

NESUG 2010

NESUG 2010 Foundations and Fundamentals Now when pressing the F10 Key all collapsable code sections are

Foundations and Fundamentals

Now when pressing the F10 Key all collapsable code sections are expanded as below:

F10 Key all collapsable code sections are expanded as below: User-Defined SAS Abbreviations SAS Abbreviations are

User-Defined SAS Abbreviations SAS Abbreviations are actually macros that insert one or more lines of code which the user associates with a shortcut. At a minimum, you must name the shortcut. In addition, you may also define hot keys for your abbreviation. SAS Abbreviations need only be created once in a single PC SAS session. Once created, abbreviations are automatically available to all of your future SAS sessions.

The first step to implementing SAS Abbreviations is to create your abbreviation within the Tools menu of the SAS Enhanced Editor.

From the TOOLS Menu of your edit Window choose ADD Abbreviation

- 12 -

NESUG 2010

Foundations and Fundamentals

NESUG 2010 Foundations and Fundamentals OR Press Ctrl + Shift + A Either method will result

OR Press Ctrl + Shift + A

Either method will result in the appearance of the Add Abbreviation window. As an example, the following

sequence will provide the steps to create a simple abbreviation called rename. The abbreviation rename will contain the basics of the SAS syntax of the RENAME statement in conjunction with PROC SORT. There are two fields to be entered in the Add Abbreviation window. First, enter your chosen name for your abbreviation in the Abbreviation: field of the Add Abbreviation window. The abbreviation in this example has

been given the name rename.

the RENAME statement in conjunction with PROC SORT has been entered in the Text to insert for

abbreviation: field. Next Press OK to save your abbreviation.

Next, enter the text associated with your abbreviation.

The basic syntax of

text associated with y our abbreviation. The basic syntax of The second step to implementing SAS

The second step to implementing SAS Abbreviations is to invoke your abbreviation.

Window of you PC SAS session, type the name you assigned to your abbreviation in the Add Abbreviation window. Note that the abbreviation name is case sensitive so that entering the shortcut name RENAME would not invoke your newly defined shortcut. You must enter the shortcut name exactly as entered in the Add Abbreviation window. In this case, typing the shortcut rename would cause the text associated with the rename abbreviation to appear in a pop-up box as shown below.

From the Editor

cause the text associated with the rename abbreviation to appear in a pop-up box as shown

- 13 -

NESUG 2010

Foundations and Fundamentals

The third and final step in implementing your abbreviation is to accept the invocation of it once it has been

invoked.

abbreviation.

shortcut. The result being that the lines of text that you entered in the Add Abbreviation window will be inserted automatically into your Editor window. The result of the acceptance of our rename abbreviation is shown below.

Selecting TAB or ENTER will accept an abbreviation. Any other keyboard selection will reject the

Accepting the abbreviation launches the execution of the macro associated with your

launches the execution of the macro associated with your Even though abbreviations are created on an

Even though abbreviations are created on an individual PC, the SAS Enhanced Editor provides the functionality to share abbreviations with others. This feature allows sharing of a large set of abbreviations, just as easily as it does for a few abbreviations. Sharing abbreviations is accomplished by utilizing the EXPORT feature on the first machine, and then the IMPORT feature on the subsequent machine(s).

FINDING CODING ERRORS

Utilizing customized features of the Enhanced Editor

By color-coding program components in your SAS editor window you can quickly see errors such as misspelled keywords and unmatched quotes and comments. This can be accomplished by selecting specific fonts for defined and undefined keywords. For example, suppose you define the foreground color for undefined keywords to black and the defined keywords as bold blue font style. As you type in the Enhanced Editor, defined SAS keywords will automatically be formatted to bold blue as they are typed correctly. Suppose you mistyped a defined keyword so that it is unrecognized by the enhanced editor. In this case the font will remain black. As the first word in a statement is always a SAS keyword, you will easily recognize misspelled keywords instantly by the black text instead of the expected bold blue font at the beginning of a line.

To set your color preferences:

from the Menu Bar Select Tools Then Options Then Enhanced Editor Then Appearance Tab

The Enhanced Editor Options window will appear as below:

- 14 -

NESUG 2010

Foundations and Fundamentals

NESUG 2010 Foundations and Fundamentals Select the Defined Keyword under File elements and select your color

Select the Defined Keyword under File elements and select your color preferences. The color Blue in bold

font was selected as described above. then select OK.

Next, choose your color preference for the Undefined keyword and

As you type your SAS code in the editor window your color preferences will be implemented.

CONCLUSION

The SAS Enhanced Editor provides a multitude of methods for efficient program creation and review such as keyboard shortcuts, editor options and SAS Abbreviations.

REFERENCE

For more information on the Enhanced Editor, please refer to the SAS Online Doc in the “SAS Companion for Microsoft Windows” at www.sas.com

ACKNOWLEDGEMENTS

I would like to thank my manager, Amy Gillespie for her assistance and support in the development of this paper.

CONTACT INFORMATION

Kathy Harkins Merck & Co. P.O. BOX 1000 UPPER GWYNEDD, PA 19454 Phone: (267) 305 – 6819 Email: kathy_harkins@Merck.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.

- 15 -