BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
nayab_shaik
Calcite | Level 5

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

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

Or..

 

data want;
    p=1; set bm point=p;        output;
    set bm nobs=nobs point=nobs;output;
    stop;
run;

View solution in original post

3 REPLIES 3
PeterClemmensen
Tourmaline | Level 20

Do like this

 

data want;
    set bm end=last;
    if _N_=1 or last;
run;
PeterClemmensen
Tourmaline | Level 20

Or..

 

data want;
    p=1; set bm point=p;        output;
    set bm nobs=nobs point=nobs;output;
    stop;
run;
ed_sas_member
Meteorite | Level 14

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,

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 3 replies
  • 1161 views
  • 1 like
  • 3 in conversation