BookmarkSubscribeRSS Feed
Rohit_R
Obsidian | Level 7

Hi,

I have a dataset that looks like follows: I want to get the latest start for the ID, i.e. if there are 2 starts in a day for an ID, I would like to keep the later start on that day.

 

DATA have:

ID                Startdate            Startdatetime

1                1/1/2000            1/1/2000 14:12:35

1                1/1/2000            1/1/2000 14:14:35

1                1/1/2000            1/1/2000 12:12:35

1                5/1/2000            5/1/2000 14:12:35

2                1/1/2000            1/1/2000 14:12:35            

2                2/1/2000            2/1/2000 14:12:35

2                2/1/2000            2/1/2000 19:12:35

3                1/1/2000            1/1/2000 14:12:35

3                2/1/2000            2/1/2000 15:12:35

3                3/1/2000            3/1/2000 16:12:35

 

DATA Want:

ID                Startdate            Startdatetime

1                1/1/2000            1/1/2000 14:14:35

1                5/1/2000            5/1/2000 14:12:35

2                1/1/2000            1/1/2000 14:12:35            

2                2/1/2000            2/1/2000 19:12:35

3                1/1/2000            1/1/2000 14:12:35

3                2/1/2000            2/1/2000 15:12:35

3                3/1/2000            3/1/2000 16:12:35

 

 

Here is what I tried:

 

PROC SORT DATA=have;

BY ID DESCENDING StartdateTime Startdate;

RUN;

 

DATA want;

  SET have;

BY Startdate;

IF first.Startdate;

RUN;

 

The sort runs fine, however, I get an error "BY variable are not properly sorted."

 

Any ideas on getting it right? I am not sure where I am going wrong.

 

Thanks,

 

 

 

 

2 REPLIES 2
RW9
Diamond | Level 26 RW9
Diamond | Level 26

As you haven't put test data in the form of a datastep, this is untested.  Also, please avoid coding in uppercase, its shouting at me.

proc sort data=have out=want;
  by id startdate descending startdatetime;
run;
data want;
  set want;
by id startdate descending startdatetime; if first.startdate; run;
Kurt_Bremser
Super User

The by in the sort has to exactly match the by in the data step. You may add additional variables in the sort, but up to what's used in the data step they have to be identical.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 12356 views
  • 0 likes
  • 3 in conversation