Pages

SyntaxHighlighter

Friday, August 27, 2021

Last Column of DSN

You can use the double dash ( -- ) shorthand to drop a list of columns in a data set based on names. I needed to do this for a list of data sets starting at a certain column to the very last column, but the last column name changed between data sets.

The below macro utilizes low level SAS functions to return the name of the last column. So now I can do something like this:

data x;
  set sashelp.class(drop = age -- %lastcolumn(sashelp.class));
run;


%macro lastcolumn(dsn);
  %local dsid retval;

  %let dsid = %sysfunc(open(&dsn));
  %if &dsid %then %do;
     %let retval = %sysfunc(varname(&dsid, %sysfunc(attrn(&dsid, nvars))));
     %let dsid = %sysfunc(close(&dsid));
  %end;

  &retval
%mend;

Thursday, August 19, 2021

My Snippets - SQL Snapshot

Now that SAS Studio is becomming more prevalant, I am moving away from Enterprise Guide into SAS Studio. A nice feature of SAS Studio is the ability to add your own code snippets.

While working on a project, I often find myself having to trace thru a multitude of data sets and track the number of rows and columns at each step. I typically keep all those data sets in the WORK library. I have created the below code snippet to support that type of activity.

The steps to save this snippet are to write the SQL statement as shown below then either right mouse click and select "Add to My Snippets" or click on the icon circled in red. Provide a name then click the Save button. Now you can easily recall that code snippet by clicking on Snippets, My Snippets, snap (short for snapshot).

Below is an example output from running the snap snippet. Notice that the output is sorted by the last modified date so it follows the flow of my program.