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

I have data as below:

ID visit measure Measure_Type
1 1 12 A
2 1 13 A
2 2 14 A
3 1 15 A
1 1 12 B
2 1 13 B
2 2 14 B
3 1 15 B
1 1 12 C
2 1 13 C
2 2 14 C
3 1 15 C

 

The ID 2 has two visits, and I only want to select the second visit and delete the first visit. I tried with the following code but it does not work:

data want;
set have;
by measure_type descending visit id;
if first.id;
run;

any hints? Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

First you would have to sort the data the same as the By statement you use.

But Since there are different values of visit then you wouldn't want the visit in the sort order then you may want to to use

 

by Measure_type id notsorted;

Then you would want to us IF LAST.ID.  If there is only one then the First and Last are the same record.

View solution in original post

3 REPLIES 3
ballardw
Super User

First you would have to sort the data the same as the By statement you use.

But Since there are different values of visit then you wouldn't want the visit in the sort order then you may want to to use

 

by Measure_type id notsorted;

Then you would want to us IF LAST.ID.  If there is only one then the First and Last are the same record.

Astounding
PROC Star

I think you're looking in the right forest ... here's a variation.  First, sort if the data are not already in this order:

 

proc sort data=have;

by measure_type id visit;

run;

 

Then select the last visit for a given measure_type / id combination:

 

data want;

set have;

by measure_type id visit;

if last.id;

run;

fengyuwuzu
Pyrite | Level 9

Thank you, both of you. Both worked. 

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 925 views
  • 0 likes
  • 3 in conversation