LISTSERV at the University of Georgia
Menubar Imagemap
Home Browse Manage Request Manuals Register
Previous messageNext messagePrevious in topicNext in topicPrevious by same authorNext by same authorPrevious page (September 2010, week 2)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:   Mon, 13 Sep 2010 23:08:10 +0200
Reply-To:   Magnus Mengelbier <mmr@LIMELOGIC.COM>
Sender:   "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:   Magnus Mengelbier <mmr@LIMELOGIC.COM>
Subject:   Re: Dynamic variable list expansion
In-Reply-To:   <AANLkTikBiWRSqLi9s2XrB405_nyoPNKKTWdRQ1mOD=UK@mail.gmail.com>
Content-Type:   text/plain; charset=ISO-8859-1

Hello Richard

Here is a thought that I think may lead in the right direction ... otherwise it was a good exercise. Worked around the VARNAME issue using VARNUM as it returns 0 if the variable name does not exist in the data set set.

%macro ExpandVarlist(data=, varlist=); %local dsid index

i data_variables check return_list ;

%* initialise variable list(s) empty ; %let data_variables = ; %let return_list = ;

%* get list of variables in the input data set ; %let dsid = %sysfunc(open(&data)); %if ( &dsid > 0 ) %then %do;

%do index = 1 %to %sysfunc(attrn(&dsid,nvars));

%let data_variables = &data_variables %sysfunc(varname(&dsid, &index)); %end; %let dsid = %sysfunc(close(&dsid));

%let dsid = 0; %end; %else %do; %put %str(ER)ROR: Could not open data set &data..; %goto exit; %end;

%* open "filtered" data set ;

%let dsid = %sysfunc(open( &data(keep=&varlist) ));

%if ( &dsid > 0 ) %then %do; %let i = 1 ; %let variable_name = %scan( &data_variables, &i, %str( )); %do %while ( &variable_name ^= %str() ) ; %let check = %sysfunc( varnum( &dsid, &variable_name )) ; %if ( &check > 0 ) %then %let return_list = &return_list &variable_name ; %let check = 0; %* get next variable name that was in the original data set ; %let i = %eval(&i + 1); %let variable_name = %scan( &data_variables, &i, %str( )) ; %end;

%let dsid = %sysfunc(close(&dsid));

%let dsid = 0; %end;

%exit: %* return list of variables ; %put &return_list ; %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);

Hope this helps

Magnus

-- _______________________________________________________________________ Magnus Mengelbier (mmr@limelogic.com) Limelogic Ltd www.limelogic.com On 13 September 2010 21:18, 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 >


Back to: Top of message | Previous page | Main SAS-L page