BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
blackandwhite
Obsidian | Level 7

Hi,

 

Participants have varying number of visits/observations in this dataset. Var is binary (0/1).

 

I am trying to delete observations as of the first time each participant has consecutive 0s for var for >24 months (as seen in the image)

 

Thanks very much

 

 

 

HAVE   WANT  
       
IDvarDate IDvarDate
1128/10/2014 1128/10/2014
1026/01/2015 1026/01/2015
1123/04/2015 1123/04/2015
1021/07/2015 1021/07/2015
1021/10/2015 1021/10/2015
1020/01/2016 1020/01/2016
1006/05/2016 1006/05/2016
1005/08/2016 1005/08/2016
1008/11/2016 1008/11/2016
1008/02/2017 1008/02/2017
1008/05/2017 1008/05/2017
1021/08/2017 2105/03/2012
1017/01/2018 2017/07/2012
2105/03/2012 2129/07/2013
2017/07/2012 2128/10/2014
2129/07/2013    
2128/10/2014    
1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

This minor change should suffice. Let me know at your earliest convenience.

 

 
data want;
set have;
k=lag(date);
by id no notsorted;
k1=intck('month',k,date);
if first.id then flag=.;
if first.id or first.no and no=0  then do;
k=0;
k1=0; 
k2=0;
end;
retain flag;
if no=0 then  k2+k1;
if k2>=24 then flag=1;
if not flag;
drop k: flag;
run;

View solution in original post

10 REPLIES 10
novinosrin
Tourmaline | Level 20

data HAVE;	 	 	 	 	 
	 
input ID	var	Date : ddmmyy10.;
format date ddmmyy10.;
cards;
1	1	28/10/2014	 	1	1	28/10/2014
1	0	26/01/2015	 	1	0	26/01/2015
1	1	23/04/2015	 	1	1	23/04/2015
1	0	21/07/2015	 	1	0	21/07/2015
1	0	21/10/2015	 	1	0	21/10/2015
1	0	20/01/2016	 	1	0	20/01/2016
1	0	06/05/2016	 	1	0	06/05/2016
1	0	05/08/2016	 	1	0	05/08/2016
1	0	08/11/2016	 	1	0	08/11/2016
1	0	08/02/2017	 	1	0	08/02/2017
1	0	08/05/2017	 	1	0	08/05/2017
1	0	21/08/2017	 	2	1	05/03/2012
1	0	17/01/2018	 	2	0	17/07/2012
2	1	05/03/2012	 	2	1	29/07/2013
2	0	17/07/2012	 	2	1	28/10/2014
2	1	29/07/2013	 	 	 	 
2	1	28/10/2014	 
; 


data want;
set have;
k=lag(date);
by id var notsorted;
k1=intck('month',k,date);
if first.var then do;k=0;k1=0;k2=0; end;
k2+k1;
if k2>=24 and var=0 then delete;
drop k:;
run;
blackandwhite
Obsidian | Level 7

works great, thank you!

blackandwhite
Obsidian | Level 7

@novinosrin 

 

As a follow-up question, if you don't mind:

 

What if I would like to delete all following observations after the condition is met?

 

Ex: Assume ID#1 has an additional visit at the end where var=1. With the code above, this obs remains.

How would I be able to remove this and any other values that follow?

 

I tried to delete the condition "and var=0" from line 8 of your code and it doesn't seem to help with this.

 

Thanks again!

 

 

novinosrin
Tourmaline | Level 20

 @blackandwhite 

 

Try

 


data want;
set have;
k=lag(date);
by id var notsorted;
k1=intck('month',k,date);
if first.id or first.var and var=0  then do;
k=0;
k1=0; 
k2=0;
end;
if var=0 then  k2+k1;
if k2>=24 then delete;
drop k:;
run;
blackandwhite
Obsidian | Level 7

Thanks

It doesn't delete the 0s however (only the 1).

I attached an example of what I get for an ID (ex: visits 12-15 should not be included).

 

 

 

Original   Output  
DateVisit no.Var DateVisit no.Var
17-08-1111 17-08-1111
20-11-1320 20-11-1320
25-02-1430 25-02-1430
27-05-1440 27-05-1440
04-06-1550 04-06-1550
21-06-1660 23-03-18120
24-11-1670 22-06-18130
23-02-1780 25-09-18140
19-06-1790 28-01-19150
25-09-17100    
22-12-17111    
23-03-18120    
22-06-18130    
25-09-18140    
28-01-19150    
       
novinosrin
Tourmaline | Level 20

Hi @blackandwhite   Please correct my understanding:

 

1. If a set of consecutive zeros is found to have with cumulative difference in months >=24 then delete all the forthcoming records beyond that  for that id?

 

So basically the first 24 is the flag?

 

Please unmark the answer so that the thread is not solved

blackandwhite
Obsidian | Level 7

That's right thanks

 

Basically the first 24 months after consecutive 0s

novinosrin
Tourmaline | Level 20

This minor change should suffice. Let me know at your earliest convenience.

 

 
data want;
set have;
k=lag(date);
by id no notsorted;
k1=intck('month',k,date);
if first.id then flag=.;
if first.id or first.no and no=0  then do;
k=0;
k1=0; 
k2=0;
end;
retain flag;
if no=0 then  k2+k1;
if k2>=24 then flag=1;
if not flag;
drop k: flag;
run;
blackandwhite
Obsidian | Level 7

Thanks so much!

Will confirm within 24h max

blackandwhite
Obsidian | Level 7

works great, thank you!!

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
  • 10 replies
  • 1426 views
  • 1 like
  • 2 in conversation