Desktop productivity for business analysts and programmers

extract the first record for each observation

Reply
N/A
Posts: 0

extract the first record for each observation

How do i extract the first record post grouping the observations in EG.

Thanks & would appreciate any help
Cheers
ALT
N/A
Posts: 0

Re: extract the first record for each observation

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.
Super Contributor
Posts: 260

Re: extract the first record for each observation

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.
Ask a Question
Discussion stats
  • 2 replies
  • 139 views
  • 0 likes
  • 2 in conversation