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

I have following sample dataset that is sorted by ID and Class Start Date. For an "ID", I would like to keep those rows whenever ID has a NEW "Class Start Date" for the same or different "Class". When doing this, I want to keep the row with the earliest "Date".I have added a column to show which rows I need to keep for an ID. Any help is much appreciated. Thank You.

 

DateIDClassClass Start DateClass End DateDays in Class Months in CollegeMonth CategoryKeep or Delete
2018-081AAA2018-08-139999-12-31632013-24 MonthsKeep
2018-091AAA2018-08-139999-12-31632113-24 Months 
2018-101AAA2018-08-139999-12-31632113-24 Months 
2018-082BBB2018-08-222018-09-304010-12 MonthsKeep
2018-092BBB2018-08-222018-09-304020-12 Months 
2017-103CCC2017-10-022017-10-222100-12 MonthsKeep
2017-103BBB2017-10-232017-11-283700-12 MonthsKeep
2017-113BBB2017-10-232017-11-283710-12 Months 
2017-113BBB2017-11-302018-02-289110-12 MonthsKeep
2017-123BBB2017-11-302018-02-289120-12 Months 
2018-013BBB2017-11-302018-02-289130-12 Months 
2018-023BBB2017-11-302018-02-289140-12 Months 
2018-033BBB2018-03-022018-05-117140-12 MonthsKeep
2018-043BBB2018-03-022018-05-117160-12 Months 
2018-053BBB2018-03-022018-05-117170-12 Months 
2018-084AAA2018-08-072018-08-312580-12 MonthsKeep
2018-094AAA2018-09-082018-09-302390-12 MonthsKeep
2017-035BBB2017-03-082017-03-12510-12 MonthsKeep
2017-035CCC2017-03-132017-05-318010-12 MonthsKeep
2017-045CCC2017-03-132017-05-318010-12 Months 
2017-055CCC2017-03-132017-05-318010-12 Months 
2018-075AAA2018-07-022018-08-316180-12 MonthsKeep
2018-085AAA2018-07-022018-08-316190-12 Months 
2017-106BBB2017-10-162017-11-304610-12 MonthsKeep
2017-116BBB2017-10-162017-11-304620-12 Months 
2017-126BBB2017-12-042018-04-3014830-12 MonthsKeep
2018-016BBB2017-12-042018-04-3014840-12 Months 
2018-026BBB2017-12-042018-04-3014850-12 Months 
2018-036BBB2017-12-042018-04-3014860-12 Months 
2018-046BBB2017-12-042018-04-3014870-12 Months 
2018-017CCC2018-01-122018-01-312000-12 MonthsKeep
2017-038CCC2017-03-152017-05-115800-12 MonthsKeep
2017-048CCC2017-03-152017-05-115800-12 Months 
2017-058CCC2017-03-152017-05-115810-12 Months 
2017-058BBB2017-05-122017-08-3111210-12 MonthsKeep
2017-068BBB2017-05-122017-08-3111220-12 Months 
2017-078BBB2017-05-122017-08-3111230-12 Months 
2017-088BBB2017-05-122017-08-3111240-12 Months 
2017-118BBB2017-11-202018-03-3113250-12 MonthsKeep
2017-128BBB2017-11-202018-03-3113260-12 Months 
2018-018BBB2017-11-202018-03-3113270-12 Months 
2018-028BBB2017-11-202018-03-3113280-12 Months 
2018-038BBB2017-11-202018-03-3113280-12 Months 
2017-119AAA2017-11-102017-12-3152120-12 MonthsKeep
2017-129AAA2017-11-102017-12-31521313-24 Months 
2018-049AAA2018-04-302018-07-05671613-24 MonthsKeep
2018-059AAA2018-04-302018-07-05671713-24 Months 
2018-069AAA2018-04-302018-07-05671813-24 Months 
2018-079AAA2018-04-302018-07-05671913-24 Months 
2018-0310AAA2018-03-262018-08-311597261+ MonthsKeep
2018-0410AAA2018-03-262018-08-311597361+ Months 
2018-0510AAA2018-03-262018-08-311597461+ Months 
2018-0610AAA2018-03-262018-08-311597561+ Months 
2018-0710AAA2018-03-262018-08-311597661+ Months 
2018-0810AAA2018-03-262018-08-311597761+ Months 
2018-0910AAA2018-09-032018-09-0537861+ MonthsKeep
2017-0611BBB2017-06-052017-06-302610-12 MonthsKeep
2017-1011CCC2017-10-232018-01-3110110-12 MonthsKeep
2017-1111CCC2017-10-232018-01-3110110-12 Months 
2017-1211CCC2017-10-232018-01-3110110-12 Months 
2018-0111CCC2017-10-232018-01-3110110-12 Months 
2017-0512BBB2017-05-302017-06-04610-12 MonthsKeep
2017-0612BBB2017-05-302017-06-04620-12 Months 
2017-0612CCC2017-06-052017-07-285420-12 MonthsKeep
2017-0712CCC2017-06-052017-07-285420-12 Months 
1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Sort by id, <class start date> and date. Then do a data step with by id and <class start date>, and use a subsetting if first.<class start date>

Insert the real names ("class start date" is not a valid SAS name).

View solution in original post

4 REPLIES 4
Kurt_Bremser
Super User

Sort by id, <class start date> and date. Then do a data step with by id and <class start date>, and use a subsetting if first.<class start date>

Insert the real names ("class start date" is not a valid SAS name).

d0816
Quartz | Level 8

Thank you so much. It worked.

d0816
Quartz | Level 8

HI,

I have similar problem but here I am trying to keep all rows with different session for an ID . IF an ID have multiple rows with same session name then, keep the row with the earliest session begin date for an ID.

For e.g for ID #3,5,7 having multiple rows with same session name, keep the row with the earliest begin date. For ID #6, with different session name, keep both the rows. Any suggestion?

DateIDSessionSessionBeginDateTotalDaysinSessionKeep or Delete
2018-081MMM2018-08-1363Keep
2018-082OOO2018-08-2240Keep
2017-113OOO2017-11-30199Keep
2018-033OOO2018-03-02199 
2017-103OOO2017-10-23199 
2018-034MMM2018-03-0741Keep
2018-085MMM2018-08-0748Keep
2018-095MMM2018-09-0848 
2018-076MMM2018-07-0261Keep
2017-036OOO2017-03-085Keep
2017-107OOO2017-10-16194Keep
2017-127OOO2017-12-04194 
2017-118OOO2017-11-20244Keep
2017-058OOO2017-05-12244 
Kurt_Bremser
Super User

@d0816 wrote:

HI,

I have similar problem but here I am trying to keep all rows with different session for an ID . IF an ID have multiple rows with same session name then, keep the row with the earliest session begin date for an ID.

For e.g for ID #3,5,7 having multiple rows with same session name, keep the row with the earliest begin date. For ID #6, with different session name, keep both the rows. Any suggestion?

Date ID Session SessionBeginDate TotalDaysinSession Keep or Delete
2018-08 1 MMM 2018-08-13 63 Keep
2018-08 2 OOO 2018-08-22 40 Keep
2017-11 3 OOO 2017-11-30 199 Keep
2018-03 3 OOO 2018-03-02 199  
2017-10 3 OOO 2017-10-23 199  
2018-03 4 MMM 2018-03-07 41 Keep
2018-08 5 MMM 2018-08-07 48 Keep
2018-09 5 MMM 2018-09-08 48  
2018-07 6 MMM 2018-07-02 61 Keep
2017-03 6 OOO 2017-03-08 5 Keep
2017-10 7 OOO 2017-10-16 194 Keep
2017-12 7 OOO 2017-12-04 194  
2017-11 8 OOO 2017-11-20 244 Keep
2017-05 8 OOO 2017-05-12 244  

Use the same method as previous, just with different variables (session!) in the by statements and the first. condition.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1287 views
  • 0 likes
  • 2 in conversation