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 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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