| Date: | Tue, 14 Sep 2010 07:15:29 -0400 |
| Reply-To: | Søren Lassen <s.lassen@POST.TELE.DK> |
| Sender: | "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU> |
| From: | Søren Lassen <s.lassen@POST.TELE.DK> |
| Subject: | Re: Dynamic variable list expansion |
|
| Content-Type: | text/plain; charset=ISO-8859-1 |
Richard,
I wrote a macro like that once, it went something like this:
%macro varlist(data,keep=,drop=);
%local dsid dskeep dsdrop i rc name;
%let dsid=%sysfunc(open(&data));
%let dskeep=%sysfunc(open(&data(keep=&keep)));
%let dsdrop=%sysfunc(open(&data(drop=&drop)));
%do i=1 %to %sysfunc(attrn(&dsid,nvars));
%let name=%sysfunc(varname(&dsid,&i));
%if %sysfunc(varnum(&dskeep,&name)) and
%sysfunc(varnum(&dsdrop,&name)) %then
%do; &name%end;
%end;
%let rc=%sysfunc(close(&dsid));
%let rc=%sysfunc(close(&dskeep));
%let rc=%sysfunc(close(&dsdrop));
%mend;
The original macro also contained some checks to see if the data set name
and the variable lists were valid, and an advanced option where=, that
could be used to subset dictionary.columns, e.g. where=name like '%num'.
The macro as is returns the intersection of the keep and drop variable
lists if both are specified.
Your macro can then be written as
%macro ExpandVarlist(data=, varlist=);
%if %length(&varlist) %then
%do;%varlist(&data,keep=&varlist)%end;
%mend;
Feel free to use whatever parts of the code that you like.
Regards,
Søren
On Mon, 13 Sep 2010 15:18:01 -0400, Richard DeVenezia
<rdevenezia@GMAIL.COM> wrote:
>I'm trying to write a macro that will return the variables that match
>a variable list.
>
>----------
>%macro ExpandVarlist(data=, varlist=);
> %local dsid index;
> %let dsid = %sysfunc(open(&data(keep=&varlist)));
> %if &dsid %then %do;
>
>%put %sysfunc(attrn(&dsid,nvars));
>
> %do index = 1 %to %sysfunc(attrn(&dsid,nvars));
>%sysfunc(varname(&dsid,&index))
> %put debug-> &index.. %sysfunc(varname(&dsid,&index)) ;
> %end;
> %let dsid = %sysfunc(close(&dsid));
> %end;
>
>%mend;
>
>data foo;
> length a b c d e f g h i j k l m n $1;
>run;
>
>%put %ExpandVarList(data=foo, varlist=d--f l--n);
>----------
>
>which bumps up against this issue http://support.sas.com/kb/1/666.html
>"DATA Step OPEN function honors the KEEP or DROP functions ...The
>VARNAME function is expecting the second parameter to be the position
>in the original dataset."
>
>Anyone have a work around or ideas ?
>Do not want to pollute the log with "WARNING: Argument 2 to function
>VARNAME referenced by the %SYSFUNC or %QSYSFUNC macro function is out
>of range."
>
>TIA,
>Richard A. DeVenezia
|