Pages

SyntaxHighlighter

Thursday, July 12, 2012

Dynamic Macro call using the RESOLVE() function

The below code uses the first dimension of a _temporary_ (not written to PDV) array as a dynamic parameter in a macro call. I was stumped on how to do this without writing a macro since the macro language is all text based.

Russ from SAS technical support suggested I use the RESOLVE() function. In order to avoid dropping the incrementor, I used the automatic _N_ variable which was inspired by Paul Dorfman in this paper.

The source code used in the %dsnobs( dsn = ) can be found here.

data rowcounts ;
  length name $16
         rows   8 ;
  array aname[ 2, 2 ] $16 _temporary_ 
    ( 
        'sashelp.cars', 'Cars Rows'
      , 'sashelp.class', 'Class Rows' 
    ) ;
  do _n_ = 1 to dim( aname ) ;
    name = aname[ _n_, 2 ] ;
    rows = resolve( cats('%dsnobs( dsn =', aname[ _n_, 1 ], ')' ) ) ;
    output ;
  end ;
run ;