Wednesday, April 25, 2012

Abbreviations and Keyboard Macros

There are times when you will use SAS code over and over and over again.  The whole copy and paste trick is fine, but sometimes you have to open up the file that you want to copy from.  There even arises the issue of trying to find the file that you want to copy from.  Using abbreviations and keyboard macros can keep your often-used code right at your fingertips (literally).



Creating Abbreviations

To create an abbreviation, start a SAS editor and from the menu bar select Tools -> Add Abbreviation...


This will display a pop-up box. Type the abbreviation name in the “Abbreviation:” box and paste the SAS code in the “Text to insert for abbreviation:” box.  In the example that I have here, I called my abbreviation "example."  The text that is associated with this abbreviation is: 
Type this line of text every time I type the word "example" in a SAS editor.  That way, I will not have to type it over and over and over again.



Now, whenever I type the word "example" in a SAS editor, SAS will turn the word red and display a small box that shows the first several characters of the saved abbreviation.


By pressing the tab key, the saved text will automatically appear.



Notice that my text is shown exactly as it is in the "Add Abbreviation" box.  I could have put all the text on one line.  I broke the lines for visual aid here.  SAS does not have a line length limit or a line count limit, so you can have your text be as long as you want, both vertically or horizontally.

If you want to use the actual word "example" in your SAS code without engaging the saved text, simply keep typing.  If you do not press tab, it will never display the saved text.



Editing Abbreviations

What if you realize that you misspelled a word or you just want to change what your text says?

From the SAS editor menu bar select Tools -> Keyboard Macros -> Macros...
(Once the abbreviation has been created, it is referred to as a "keyboard macro.")



This will display another pop-up box that lists all the keyboard macros that you have created.  Simply select the abbreviation name that you want to edit, and click on the "Edit..." button.  This will display yet another pop-up box.  Click "Modify" to get the box where you may edit anything you need.



Real-life Examples

I have created several of these keyboard macros, mostly for the purpose of recalling the parameters required for macros that I have written as well as their location to include in the SAS program.  But two that I use in every program are header comments and output code.

Header Comments

Header comments are the information that should appear at the top of every program.  It should give some detail like the name and date of the program, the name of the programmer, the production path of the program, and the purpose of the program.  It is also useful to have a history log to show who, when, and what was changed if anyone ever makes changes to the program.

I have already saved the macro text, so I will just show you how I get it and what the output looks like.

First, I named my keyboard macro "header."  So, by typing the word "header" in a SAS editor, it recognizes that I have a keyboard macro with that name and prompts me with a reminder that I can use the saved macro text:


Next, after I press the tab key, the following saved text is automatically displayed.


SIMPLE!  Now I just have to change the "XXX" placeholders, change any information that may need adjusted, and fill in any blank spaces.


Output Code

Your soul purpose as a SAS programmer is to provide output for your boss or for your clients.  I have posted a whole discussion on how to make standardized output.  Since I generate so much ODS output, I am using the same code over and over again.  This saves me a lot of time.
Again, I have already saved the macro text previously.  I named this keyboard macro "outready." So, by typing the word "outready" in the SAS editor, it recognizes that I have a keyboard macro with that name and prompts me with a reminder that I can use the saved macro text:


After I press the tab key, the following saved text is automatically displayed.




Once again, it's as simple as that!  All that is left is to change any place holders or adjust any other information.

I got all that text for a header and for output just by typing two small words.



But what if I forget what my keyboard macro is called or if I even have one saved?

This can be resolved in one simple step:

1.  Bring up the keyboard macros and search the list of names that are displayed:






When you create keyboard macros you can add a description to remind you of what the key word will do. It is also possible to assign quick keys to your keyboard macros. As you can see, I have not taken the time to do that. But be sure not to overwrite any other quick keys that you usually use.


For example, I could assign the keyboard macro "header" as ctrl+H, but that would overwrite the find-and-replace function that I use on a daily basis.  It is find to overwrite such functions as long as you are sure that you will never use them.

Have fun creating all of your own keyboard macros!








No comments: