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,

sas-innovate-wordmark-2025-midnight.png

Register Today!

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.


Register now!

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
  • 717 views
  • 1 like
  • 3 in conversation