BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
dwsmith
Obsidian | Level 7
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?

1 ACCEPTED SOLUTION

Accepted Solutions
dwsmith
Obsidian | Level 7

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;

View solution in original post

7 REPLIES 7
DanZ
Obsidian | Level 7

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;
dwsmith
Obsidian | Level 7
Where do I add that to my current set up? Or is this a new data step?
DanZ
Obsidian | Level 7
Oh, sorry. I should have put if counter = 2. Replace your check datastep.
dwsmith
Obsidian | Level 7

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.

Kurt_Bremser
Super User

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.

dwsmith
Obsidian | Level 7

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

dwsmith
Obsidian | Level 7

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;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 1088 views
  • 0 likes
  • 3 in conversation