Write and run SAS programs in your web browser

How to change the value of a variable

Accepted Solution Solved
Reply
Contributor
Posts: 25
Accepted Solution

How to change the value of a variable

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!


Accepted Solutions
Solution
a month ago
Super User
Super User
Posts: 7,417

Re: How to change the value of a variable

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


All Replies
Solution
a month ago
Super User
Super User
Posts: 7,417

Re: How to change the value of a variable

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;

Contributor
Posts: 25

Re: How to change the value of a variable

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

☑ This topic is solved.

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

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