BookmarkSubscribeRSS Feed
corkee
Calcite | Level 5

Hello,

 

I'm trying to select the first observation based on multiple BY values, using the first or last modifier. The data below are already sorted, and I really want to keep the rows that are in bold (the first column is patientID, second is episodekey, and third is visitkey). Essentially, some patientID's have multiple VISITKEY's linked to the same EPISODEKEY. For these subjects, I want to keep the unique episodes, after sorting the data appropriately. 

 

This is the code I've used to sort the data:

 

proc sort data=fix2; 
by ncdrpatientid episodekey visitkey descending died_yn descending f_assessmentdate descending dcdate descending tvtprocedurestopdate;
run;

I'm not sure if this works. Can anyone provide some guidance?

data fix_final;
	set fix2;
	by ncdrpatientid episodekey visitkey descending died_yn descending f_assessmentdate descending dcdate descending tvtprocedurestopdate;
	if first.episodekey;
run;
376272213206613026005JUL2017Alive29JUN201729JUN201709JUL2018.Alive.02
376272213206613026005JUL2017Alive29JUN201729JUN201708AUG2017.Alive.02
376272213206613026505JUL2017Alive30JUN201730JUN201709JUL2018.Alive.02
376272213206613026505JUL2017Alive30JUN201730JUN201708AUG2017.Alive.02
3762722646006360717JAN2016Alive15JAN201615JAN201623JAN2017.Alive.02
3762722646006360717JAN2016Alive15JAN201615JAN201629FEB2016.Alive.02
2 REPLIES 2
ballardw
Super User

It depends on what you mean by "works".

 

The shown FIRST. value will select the first record for each level of the Episodekey variable within each patient id.

corkee
Calcite | Level 5

Hello!

 

Based on what you said, it sounds exactly what I'm looking for. Thanks for the clarification!

 

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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
  • 2 replies
  • 1398 views
  • 0 likes
  • 2 in conversation