Hi all,
I am working on SAS University Edition v.9.4. and I need your help to change the value of a variable across time. I know it is an easy task, but I am a SAS beginner user.
I will first provide you with a small datastep of my dataset. So that you can play a bit around with it!
data have;
input customer_id_ano year month customer_status_numeric;
cards;1 2017 1 0
1 2017 2 0
1 2017 3 0
1 2017 4 0
1 2017 5 0
1 2017 6 1
1 2017 7 1
1 2017 8 0
1 2017 9 0
1 2017 10 1
1 2017 11 1
1 2017 12 0
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 0
2 2017 11 0
2 2017 12 0
3 2017 1 0
3 2017 2 0
3 2017 3 0
3 2017 4 0
3 2017 5 0
3 2017 6 0
3 2017 7 0
3 2017 8 0
3 2017 9 0
3 2017 10 0
3 2017 11 0
3 2017 12 0
4 2017 1 1
4 2017 2 1
4 2017 3 1
4 2017 4 1
4 2017 5 1
4 2017 6 1
4 2017 7 1
4 2017 8 0
4 2017 9 0
4 2017 10 0
4 2017 11 1
4 2017 12 1
5 2017 1 1
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;I need a code that can perform the following task:
- When the variable customer_status_numeric turns into 1 I would like it to be 1 across the remaining following periods. In other words, as soon as customer_id _ano 1 turns into being 1 (customer_status_numeric=1) in June 2017 (as displayed in the data step above) I want him/her to stay 1 for the rest of the following months. Thus, customer_status_numeric should not turn into 0 in August 2017, but remain 1. And the same for the rest of the customers.
I need this variable customer_status_numeric not to reset into 0 again once it has changed its value into 1.
I hope I´ve provided you with a clear picture of what my issue is.
Thank you in advance and do not hesitate to ask further questions if you need to!
Something like:
data have; input customer_id_ano year month customer_status_numeric; cards; 1 2017 1 0 1 2017 2 0 1 2017 3 0 1 2017 4 0 1 2017 5 0 1 2017 6 1 1 2017 7 1 1 2017 8 0 1 2017 9 0 1 2017 10 1 1 2017 11 1 1 2017 12 0 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 0 2 2017 11 0 2 2017 12 0 3 2017 1 0 3 2017 2 0 3 2017 3 0 3 2017 4 0 3 2017 5 0 3 2017 6 0 3 2017 7 0 3 2017 8 0 3 2017 9 0 3 2017 10 0 3 2017 11 0 3 2017 12 0 ; run; data want (drop=customer_status_numeric rename=(c=customer_status_numeric)); set have; by customer_id_ano; retain c; if first.customer_id_ano then c=0; if customer_status_numeric=1 then c=1; run;
Something like:
data have; input customer_id_ano year month customer_status_numeric; cards; 1 2017 1 0 1 2017 2 0 1 2017 3 0 1 2017 4 0 1 2017 5 0 1 2017 6 1 1 2017 7 1 1 2017 8 0 1 2017 9 0 1 2017 10 1 1 2017 11 1 1 2017 12 0 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 0 2 2017 11 0 2 2017 12 0 3 2017 1 0 3 2017 2 0 3 2017 3 0 3 2017 4 0 3 2017 5 0 3 2017 6 0 3 2017 7 0 3 2017 8 0 3 2017 9 0 3 2017 10 0 3 2017 11 0 3 2017 12 0 ; run; data want (drop=customer_status_numeric rename=(c=customer_status_numeric)); set have; by customer_id_ano; retain c; if first.customer_id_ano then c=0; if customer_status_numeric=1 then c=1; run;
@RW9 Exactly what I meant. Thank you!!!
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Get started using SAS Studio to write, run and debug your SAS programs.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.