Write and run SAS programs in your web browser

How to detect an event on SAS and how to remove the observations that follow that event

Accepted Solution Solved
Reply
Contributor
Posts: 25
Accepted Solution

How to detect an event on SAS and how to remove the observations that follow that event

Hi all,

I am using SAS University Edition v.9.4. and I am working on a dataset that detect when a customer is about to churn from a bank.

Now, I need the following task to be performed by SAS and hoefully you can help. But first, let me provide you with a small datastep of my sample:

data have;
input customer_id_ano year month churn;
cards;
1 2017 1 0
1 2017 2 0
1 2017 3 0
1 2017 4 0
1 2017 5 0
1 2017 6 0
1 2017 7 0
1 2017 8 0
1 2017 9 1
1 2017 10 1
1 2017 11 1
1 2017 12 1
2 2017 1 0
2 2017 2 0
2 2017 3 0
2 2017 4 1
2 2017 5 1
2 2017 6 1
2 2017 7 1
2 2017 8 1
2 2017 9 1
2 2017 10 1
2 2017 11 1
2 2017 12 1
3 2017 1 1
3 2017 2 1
3 2017 3 1
3 2017 4 1
3 2017 5 1
3 2017 6 1
3 2017 7 1
3 2017 8 1
3 2017 9 1
3 2017 10 1
3 2017 11 1
3 2017 12 1
4 2017 1 0
4 2017 2 0
4 2017 3 0
4 2017 4 1
4 2017 5 1
4 2017 6 1
4 2017 7 1
4 2017 8 1
4 2017 9 1
4 2017 10 1
4 2017 11 1
4 2017 12 1
5 2017 1 0
5 2017 2 1
5 2017 3 1
5 2017 4 1
5 2017 5 1
5 2017 6 1
5 2017 7 1
5 2017 8 1
5 2017 9 1
5 2017 10 1
5 2017 11 1
5 2017 12 1
;
run;

Now, what I need is SAS to detect when the variable churn is 1 for the first time and delete all the observations following that event. In other words, instead of a list of 1s following the first churn event, I only need to keep the first 1 and delete the remaining ones. 

Let´s take customer_id_ano 1 as an example. We can see from the data step above that the variable churn for this customer is 1 starting from September 2017 and it remains 1 until the end of the observation period. Well, I only want the first churn=1 that occurred in September to remain in my dataset and the following observation must be dropped from it. 

I hope I managed to explai myself. And I would like to thank you in advance for the help you always provide to all the SAS beginners like me. Very appreciated!!


Accepted Solutions
Solution
‎07-28-2017 03:51 PM
Super User
Posts: 7,757

Re: How to detect an event on SAS and how to remove the observations that follow that event

Would this solve your task?

data want;
set have;
by customer_id_ano churn notsorted;
if churn = 0 or first.churn;
run;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Solution
‎07-28-2017 03:51 PM
Super User
Posts: 7,757

Re: How to detect an event on SAS and how to remove the observations that follow that event

Would this solve your task?

data want;
set have;
by customer_id_ano churn notsorted;
if churn = 0 or first.churn;
run;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 25

Re: How to detect an event on SAS and how to remove the observations that follow that event

Posted in reply to KurtBremser

thank you @KurtBremser! I will acknowledge you in my thesis :-D

☑ This topic is solved.

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

Discussion stats
  • 2 replies
  • 132 views
  • 1 like
  • 2 in conversation