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

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!

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

View solution in original post

2 REPLIES 2
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

noemi_b
Obsidian | Level 7

@RW9 Exactly what I meant. Thank you!!!

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

SAS Enterprise Guide vs. SAS Studio

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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