Hi Team,
data bm;
input id sex$ age;
cards;
10 m 45
11 m 12
12 f 25
13 f 30
14 m 14
15 f 30
16 m 41
17 f 19
18 f 21
run;
i need only first observation and last observation by using first.last.
Thanks and advance
Or..
data want;
p=1; set bm point=p; output;
set bm nobs=nobs point=nobs;output;
stop;
run;
Do like this
data want;
set bm end=last;
if _N_=1 or last;
run;
Or..
data want;
p=1; set bm point=p; output;
set bm nobs=nobs point=nobs;output;
stop;
run;
Hi @nayab_shaik
Do you need the first / last observation BY groups or for the overall dataset?
In case it is "by group", you will need:
1- To sort the data:
e.g.
proc sort data=bm;
by sex age;
run;
2- To use the first/last flag to select the data. The "BY" statement identifies the 'groups' and create 2 internal variables: last. first.
e.g.
data want;
set bm;
by sex age;
if first.sex or last.sex then output;
run;
-> in the following example, you will gather the youngest and the oldest men, and the youngest and the oldest women.
Best,
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.