BookmarkSubscribeRSS Feed
apple
Calcite | Level 5

Each unique ID have many different rows of  addresses, I want a program to select for each unqiue ID any 3 rows of addressses. 

 

Thank you.

 

Example of dataset:

 

ID  Address

1    A

1    B

1    C

2    HH

2    KK

2    NN

2    MM

 

  

3 REPLIES 3
LinusH
Tourmaline | Level 20

If the data is sorted as per your example, use data step with BY ID, and use a counter that is re-initiated for each first.ID.

Use explicit output when the counter is <= 3.

Data never sleeps
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Just a slight alteration of @LinusH answer uses lag to decide if to output or not (saves a variable and line of code):

data have;
  input id  address $;
datalines;
1    A
1    B
1    C
2    HH
2    KK
2    NN
2    MM
;
run;
data want;
  set have;
  by id;
  if first.id or lag(first.id) or lag2(first.id) then output;
run;
Ksharp
Super User
data have;
  input id  address $;
datalines;
1    A
1    B
1    C
2    HH
2    KK
2    NN
2    MM
;
run;

proc surveyselect data=have out=want sampsize=3;
strata id;
run;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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
  • 787 views
  • 3 likes
  • 4 in conversation