BookmarkSubscribeRSS Feed
lerdem
Quartz | Level 8

When i have a table like below, how can i select the second observation ?

 

crn      id     lastname

230     12     poole

230     13     sandy

230     14     cook

230      15   daniela

150     13    sandy

150     22    bob

150     18    cem

 

so i need to pick same crn's second lastname

 

crn     id      lastname

230   13      sandy

150    22     bob

 

 

thank you 

3 REPLIES 3
Steelers_In_DC
Barite | Level 11

Here is a solution:

 

data have;
input crn$ id lastname$;
cards;
230     12     poole
230     13     sandy
230     14     cook
230     15     daniela
150     13     sandy
150     22     bob
150     18     cem
;

data want;
set have;
by descending crn;
count + 1;
if first.crn then count = 1;
if count = 2 then output want;
drop count;
run;

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Hi,

 

data want;
  set have; 
  by crn;
  if first.crn then temp_obs=1;
  else temp_obs=temp_obs+1;
  if temp_obs=2 then output;
run;

ASsumes the data is sorted by crn, you can change the 2 to be any obs.

FreelanceReinh
Jade | Level 19

Or you could try this:

data want;
do _n_=1 by 1 until(last.crn);
  set have;
  by crn notsorted;
  if _n_=2 then output;
end;
run;

This does not assume that HAVE is sorted by (ascending or descending) CRN, but it must be grouped in the sense that observations with the same value of CRN form a block of contiguous observations.

 

Is it okay that CRNs with only one observation in HAVE do not occur in WANT?

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

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

View all other training opportunities.

Discussion stats
  • 3 replies
  • 1018 views
  • 0 likes
  • 4 in conversation