A Room with a Nu
A Room with a Gnu
Original room image credit jaybergesen (source).
Friday, March 18, 2011
Wednesday, March 16, 2011
SAS Macros versus Python Functions, Part 1
I recently started reading Dive Into Python, a primer to the programming language Python (which I converted to MOBI and loaded onto my Kindle - available here), and I was delightfully surprised how similar functions are to the SAS macro. Here are some examples:
First, the very first line of a function or macro, that defines it as a function/macro, is very similar. In Python, one would write:
def buildConnectionString(params):
In SAS, it is very similar:
%macro buildConnectionString(params);
The terms def and %macro begin the code by stating what follow: a function definition or a macro. The "buildConnectionString" is the name of the function/macro, and enclosed in parentheses are the function/macro parameters or arguments, separated by commas.
Second, it is often a good idea to have some documentation on the function/macro. In Python, the documentation is stored in a doc string; in SAS, a very short and not as useful description string is available:
def buildConnectionString(params):
"""Build a connection string from a dictionary of parameters.
Returns a string."""
SAS:
%macro buildConnectionString(params) / des="Build connection string. Returns string.";
UPDATE: I'm not sure where I learned the 40-character limit mentioned below, but I just read in the SAS help documentation that the limit is 256 characters. Nonetheless, reading the description is still limited by screen space, so the alternatives I mentioned here are still very useful.
Unfortunately, SAS macros are limited to40 256 characters in the description. This can be worked around by offering HELP as a valid value to the first argument, which then outputs additional information about the macro. My macros are documented in a spreadsheet, and I have a macro (ArgList) that reads in the spreadsheet and outputs additional details about the arguments when I need it. One day, I would like to convert all of my macros to use a standard header that contains the information about the arguments, its purpose, and other details in a standard way so I can modify the ArgList macro to read the macro itself instead of external documentation. In Python, though, the doc string can be viewed when referencing the function, on the fly. That's incredibly useful, and much more flexible than SAS.
One area of difference is the syntax and the meaning of space: In all Python code, space has meaning, whereas in SAS code in general, it does not. Even so, I typically indent my SAS code just like Python code requires (in most cases - I try to maintain readability, too).
Once I learn more about Python, I'll post more comparisons!
First, the very first line of a function or macro, that defines it as a function/macro, is very similar. In Python, one would write:
def buildConnectionString(params):
In SAS, it is very similar:
%macro buildConnectionString(params);
The terms def and %macro begin the code by stating what follow: a function definition or a macro. The "buildConnectionString" is the name of the function/macro, and enclosed in parentheses are the function/macro parameters or arguments, separated by commas.
Second, it is often a good idea to have some documentation on the function/macro. In Python, the documentation is stored in a doc string; in SAS, a very short and not as useful description string is available:
def buildConnectionString(params):
"""Build a connection string from a dictionary of parameters.
Returns a string."""
SAS:
%macro buildConnectionString(params) / des="Build connection string. Returns string.";
UPDATE: I'm not sure where I learned the 40-character limit mentioned below, but I just read in the SAS help documentation that the limit is 256 characters. Nonetheless, reading the description is still limited by screen space, so the alternatives I mentioned here are still very useful.
Unfortunately, SAS macros are limited to
One area of difference is the syntax and the meaning of space: In all Python code, space has meaning, whereas in SAS code in general, it does not. Even so, I typically indent my SAS code just like Python code requires (in most cases - I try to maintain readability, too).
Once I learn more about Python, I'll post more comparisons!
Subscribe to:
Posts (Atom)