Learning SAS? Welcome to the exclusive online community for all SAS learners.

Ignore observations that occur within 20 minutes

Accepted Solution Solved
Reply
New Contributor
Posts: 4
Accepted Solution

Ignore observations that occur within 20 minutes

Hi,

I have a dataset with repeating IDs and a datetime variable.

It looks like this

IDDateTime
12301/Jul/2014 15:08:15
12301/Jul/2014 15:38:15
12301/Jul/2014 15:40:15
12301/Jul/2014 15:55:15
12301/Jul/2014 15:59:15
45601/Jul/2014 15:10:15

And I want the output like this

ID

DateTime

12301/Jul/2014 15:08:15
12301/Jul/2014 15:38:15
12301/Jul/2014 15:59:15
45601/Jul/2014 15:10:15

Please help. Thanks.


Accepted Solutions
Solution
‎04-25-2015 03:13 PM
PROC Star
Posts: 7,363

Re: Ignore observations that occur within 20 minutes

Then I'd suggest something like:

data want (drop=lastdt);

  set have;

  by id;

  retain lastdt;

  if first.id then do;

    lastdt=datetime;

    output;

  end;

  else if datetime-lastdt ge 20*60 then do;

    output;

    lastdt=datetime;

  end;

run;

View solution in original post


All Replies
Super User
Posts: 17,842

Re: Ignore observations that occur within 20 minutes

Assuming these are SAS datetime variables and not character variables use the dif function.

Datetime variables are measured in seconds so 60*20=1200 seconds is your cutoff.

UNTESTED:

data want;

set have;

by id datetime;

dif_time=dif(datetime);

if not first.id and dif_time<1200 then delete;

run;

New Contributor
Posts: 4

Re: Ignore observations that occur within 20 minutes

Hi Reeza,

I have just edited the output that I actually need. Made a mistake in that previously.

The problem in using the dif function is !5:59 is getting deleted as dif compares the previous value.

Thanks.

PROC Star
Posts: 7,363

Re: Ignore observations that occur within 20 minutes

What is your rationale for keeping: 01/ju/2014 15:52:15 ?

Plus, do you want within 20 minutes of the last record, or within 20 minutes of the last kept record?

New Contributor
Posts: 4

Re: Ignore observations that occur within 20 minutes

Hi Arthur,

I have just edited the required output now. 01/Jul/15 15:52:15 record was not supposed to come in the output.

Thanks.

Solution
‎04-25-2015 03:13 PM
PROC Star
Posts: 7,363

Re: Ignore observations that occur within 20 minutes

Then I'd suggest something like:

data want (drop=lastdt);

  set have;

  by id;

  retain lastdt;

  if first.id then do;

    lastdt=datetime;

    output;

  end;

  else if datetime-lastdt ge 20*60 then do;

    output;

    lastdt=datetime;

  end;

run;

New Contributor
Posts: 4

Re: Ignore observations that occur within 20 minutes

Thank you. It worked exactly as required. Smiley Happy Thanks a lot.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 411 views
  • 7 likes
  • 3 in conversation