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,
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.