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`.
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!
data &out(drop=ahuigeID3498273456);
set &out;
by &by;
if first.%scan(&by,%AHGcount(&by)) then ahuigeID3498273456=1;
else ahuigeID3498273456+1;
if &m<ahuigeID3498273456<=&n then output;
run;
1
DATA &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;
7
RUN;
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.