Published on :
Macro EXTERNAL

Macro AHGkeepN - Sequential filtering by group

This code is also available in: Deutsch Español Français
Awaiting validation
The `AHGkeepN` macro filters a dataset to keep only the first N observations (or a range defined by m and n) for each unique combination of variables specified in the `by` parameter. It first sorts the data, then uses an internal counter within a Data Step to select the desired rows. It appears to depend on external utility macros (`%AHGblank`, `%AHGbarename`, `%AHGcount`) not defined in this script.
Data Analysis

Type : EXTERNAL


The dataset is dynamically specified via the `dsn` parameter when calling the macro.

1 Code Block
PROC SORT Data
Explanation :
Sorts the input table (`dsn`) according to the grouping variables (`by`) and stores the result in the output table (`out`). If `out` is not specified, a name is derived from `dsn`.
Copied!
1%IF %AHGblank(&out) %THEN %let out=%AHGbarename(&dsn);
2PROC SORT DATA=&dsn out=&out;
3 BY &BY;
4RUN;
2 Code Block
DATA STEP Data
Explanation :
Reads the sorted table and uses a temporary counter (`ahuigeID3498273456`) to number observations within each group defined by the last variable in the `by` list. Only observations whose rank is strictly greater than `m` and less than or equal to `n` are retained.
Copied!
1DATA &out(drop=ahuigeID3498273456);
2 SET &out;
3 BY &BY;
4 IF first.%scan(&BY,%AHGcount(&BY)) THEN ahuigeID3498273456=1;
5 ELSE ahuigeID3498273456+1;
6 IF &mTHEN OUTPUT;
7RUN;
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.