turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- Base SAS Programming
- /
- Using first. and last. to determine sections of da...

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

03-10-2016 03:42 PM

```
data test;
format dt date9;
format dt2 date9;
input dt dt2;
datalines;
20000 20001
20000 20002
20000 20003
21000 21001
21000 21002
21000 21003
21000 21004
21000 21005
;
run;
proc sort data = test;
by dt dt2;
run;
data check;
set test;
by dt dt2;
if last.dt = first.dt then
if abs(last.dt2 - first.dt) < 5 then delete;
run;
```

What I would like to happen is the section of

20000 20001 20000 20002 20000 20003

dates to all be deleted and the next section

21000 21001 21000 21002 21000 21003 21000 21004 21000 21005

to all be retained. How can I get this to work?

Accepted Solutions

Solution

03-11-2016
08:26 AM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to dwsmith

03-11-2016 08:26 AM

Here is a solution I have that worked now:

```
data check;
set test;
by dt dt2;
format dt dt2 date9.;
diff = abs(dt - dt2);
if diff < 5 then delete;
if last.dt then output;
run;
```

All Replies

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to dwsmith

03-10-2016 03:51 PM - edited 03-10-2016 03:55 PM

You can assign a counter variable, and limit the output to the first group of dt.

```
data check;
set test;
by dt dt2;
```

format dt dt2 mmddyy10.;
if first.dt then counter+1;
if counter=2;
run;

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to DanZ

03-10-2016 03:53 PM

Where do I add that to my current set up? Or is this a new data step?

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to dwsmith

03-10-2016 03:57 PM

Oh, sorry. I should have put if counter = 2. Replace your check datastep.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to DanZ

03-11-2016 07:31 AM - edited 03-11-2016 07:49 AM

Your suggestion doesn't yield the desired outcome. This is also a toy problem so we won't have the luxury of saying counter occurrence # is desired.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to dwsmith

03-11-2016 03:19 AM

This condition:

`if last.dt = first.dt`

can only be true when there is only __one__ observation in the current BY (dt) group. When you process the last observation of a by group containing multiple observations, only last. is true, and in the next observation only first. will be true.

---------------------------------------------------------------------------------------------

Maxims of Maximally Efficient SAS Programmers

Maxims of Maximally Efficient SAS Programmers

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to KurtBremser

03-11-2016 07:32 AM

How can I do what I am looking to do though?

Solution

03-11-2016
08:26 AM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to dwsmith

03-11-2016 08:26 AM

Here is a solution I have that worked now:

```
data check;
set test;
by dt dt2;
format dt dt2 date9.;
diff = abs(dt - dt2);
if diff < 5 then delete;
if last.dt then output;
run;
```