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!!
Would this solve your task?
data want;
set have;
by customer_id_ano churn notsorted;
if churn = 0 or first.churn;
run;
Would this solve your task?
data want;
set have;
by customer_id_ano churn notsorted;
if churn = 0 or first.churn;
run;
thank you @Kurt_Bremser! I will acknowledge you in my thesis 😄
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.