Tuesday 25 February 2014

Use Macro Abbreviations for Shortcuts

One of my biggest issues with SAS code is remembering the syntax of the code.  For example, when you reference a field in proc freq the syntax is "table" but in proc sort the field is referenced by the syntax "by".  With macro abbreviations you won't have to remember these inconsistencies of SAS code.  I mentioned the macro abbreviations in my previous blog, but I want to explore how I use them for a lot of little code that I use everyday.


 Using the Macro Abbreviations


  1. In the SAS editor go to Tools -> Add Abbreviation
  2. Under Abbreviation enter a string of text you want to use for the shortcut.  This is case sensitive so I suggest you type the shortcut in the case you usually code in.
  3. Under Text to insert for abbreviation, type in the text you want to appear after you type your shortcut.
I use this tool for all sorts of code.  When I type the text "freq" I get the text



Proc Freq Data_____;
      Table ______;
run;


The underline is very handy because they are a visual reminder what information you need  to add and it is easy to highlight with a double click.

I use SAS sql as my main query language and it is great to start every query by typing "sql" hit tab and I get the following text.


proc sql;
      create table _____ as
            select *
      from ____
      where 
      _____
      ;
quit;


It is great for code you don't use very often and can't remember the syntax.  I have a tough time remembering the syntax for delete so I created a macro abbreviation for the text "delete" and the following text appears.


proc sql;
      delete
      from _____
      where Field_Name in (Select field_name from OtherTable)
      ;
quit;

This code deletes from a table where the value of one field equals the same field from another table.  It is very handy when you create a control table and want to ensure that customers in the control are not in the campaign table.

The great thing about macro abbreviations is there is no limit to the volume of code that can go into the "Text to insert for abbreviation" box.  I have put pages of code in that box because I have had situations where certain projects started with exactly the same code.

I would love to hear some of the ways you use the macro abbreviation to make your programming efficient.





No comments:

Post a Comment