Hi, I modified your code as following. Basically I selected all variables because I needed other variables in the original dataset which I did not show in the sample dataset. proc sql; create table want1 as select *, sum(DaysInSession) as DaysInSession from have group by id,SessionName order by id,sessionname; quit; proc sql; create table want2 as select *, sum(DaysInSession) as DaysInSession from have group by id; quit; As a result, I got the following sample dataset from the first set of code. How to subset this dataset to keep only the rows with the " Keep" for an ID? So keep all rows with different session for an ID and 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
... View more