This macro dynamically generates a DATA step to filter variables from an input table (`dsn`). Instead of listing variable names, the user provides a list of indices (`IDs`). The macro relies on third-party macros (`AHGvarlist`, `AHGblank`, `AHGbarename`, `ahgcount`) to retrieve the list of variable names and construct the appropriate `KEEP` clause.
Data Analysis
Type : MIXED
The macro processes a SAS table provided as a parameter (`dsn`). The source therefore depends on the macro call.
1 Code Block
DATA STEP Data
Explanation : This block defines the macro. It starts by determining the output table name. Then, it retrieves the list of variables from the source table via `%AHGvarlist`. The core of the processing is a `%do` loop within the `keep=` statement, which translates each numerical index provided in `IDs` into its corresponding variable name via nested `%scan` functions.
Copied!
%macro AHGkeepvar(dsn,IDs,out=);
%if %AHGblank(&out) %then %let out=%AHGbarename(&dsn);
%local i count varlist;
%AHGvarlist(&dsn,Into=varlist,dlm=%str( ),global=0);
%let count=%ahgcount(&ids);
data &out;
set &dsn(keep=
%do i=1 %to &count;
%scan(&varlist,%scan(&IDs,&i))
%end;
);
run;
%mend;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.
SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. ® indicates USA registration. WeAreCAS is an independent community site and is not affiliated with SAS Institute Inc.
This site uses technical and analytical cookies to improve your experience.
Read more.