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;

No comments:

Post a Comment