BookmarkSubscribeRSS Feed
deleted_user
Not applicable
How do i extract the first record post grouping the observations in EG.

Thanks & would appreciate any help
Cheers
ALT
2 REPLIES 2
deleted_user
Not applicable
Hello Alt,

I'm not sure if this is what you're trying to do but getting the first observation of each By group can easily be done with SAS code:

proc sort data=sashelp.prdsale out=test;
by product;run;

data prdsale;
set test;
by product;
if first.product then output;
else delete;
run;

Is this what you're trying to do?
I'm not sure if there is a built in task that does this in EG but you could add this as a code node in your process flow or insert it as custom code in your task node.
Olivier
Pyrite | Level 9
If you agree to include some code in your EG project, the previous answer is the simplest you can do. If you want to stick to EG "point-and-click" way of life, I suggest the following...
Start a query on your data.
Compute 2 new variables :
1) one named "SEQUENCE" (for example) with the following formula : MONOTONIC()
2) one named "FIRST" with the following formula : MIN(CALCULATED sequence)
The SEQUENCE variable will consist in numbers given to each data line, in a sequential way (hence its name) : 1, 2, 3, ... The FIRST variable will compute the minimum sequence number for each group. Don't forget to add in the query interface that the FIRST variable is to be computed for each group !

One last thing : in the "Group Filtering" tab, add the condition that SEQUENCE = FIRST. And then you're done.
The SAS code produced should somehow look like this :

PROC SQL ;
SELECT *,
MONOTONIC() AS order,
MIN(CALCULATED order) AS first
FROM sashelp.class
GROUP BY age
HAVING order = first
;
QUIT ;

This gives you the first child for each group of Age, from the SASHELP.CLASS dataset.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 6876 views
  • 0 likes
  • 2 in conversation