/sas-letput

A simple SAS macro to strip a macro variable and display it in the log.

Primary LanguageSAS

The %let statement is an effective way to trim leading and trailing spaces from macro variable values. The %put statement is an effective way to display macro variable values to the log. Using %let and %put together looks like this.

%let mymac = &mymac;
%put mymac = [&mymac];

Unfortunately, typing the macro variable name 4 times can become quite tedious. This repository contains a simple macro %letput that can be used to strip a macro variable and displaying its value in your log.

data _null_;
   x = "   my value   ";
   call symput("mymac",x);
run;

%put mymac = [&mymac];

%letput(mymac);

Log excerpt:

letput log excerpt

The benefits of %letput vs. %let + %put are:

  1. You only have to type mymac once.
  2. You don't have to type the [brackets].
  3. The resulting macro variable value has been stripped (spaces removed from beginning and end).
  4. The value displayed in the log is indented and color coded.

You can read more about the macro in the SESUG paper.

GOTCHA

Of course, this being the SAS macro language, nothing is perfect. If your macro variable values have double quotes in them you will get an error with %letput. Many alternative approaches were explored, but each came with its own shortcoming. It was decided that double quotes was the least likely item to show up in a macro variable value and so we live with that possibility. On the plus side, if you do have double quotes in your macro variable values, simply comment out %letput for that macro variable.