Published on :
Macro MIXED

AHGkeepvar Macro: Variable Selection by Position

This code is also available in: Deutsch Español Français
Awaiting validation
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!
1%macro AHGkeepvar(dsn,IDs,out=);
2 %IF %AHGblank(&out) %THEN %let out=%AHGbarename(&dsn);
3 %local i count varlist;
4 %AHGvarlist(&dsn,Into=varlist,dlm=%str( ),global=0);
5 %let count=%ahgcount(&ids);
6 DATA &out;
7 SET &dsn(keep=
8 %DO i=1 %to &count;
9 %scan(&varlist,%scan(&IDs,&i))
10 %END;
11 );
12 RUN;
13
14%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.