I would like to assign to some of the observations in the "ondays" variable below new values that depend on whether the variable "failure" is 0 or 1. Here my data set
ID X ONDAYS FAILURE
1 0 59 1
2 0 514 1
3 0 313 0
4 0 631 1
5 0 107 1
6 0 71 0
7 0 583 1
8 0 91 1
9 0 66 1
10 0 95 0
My goal is to sort ONDAYS---from lowest to highest (and I can do this)---to order values in ONDAYS and then create a new variable called "NEWDAYS" which will have the same value as in ONDAYS if FAILURE=1, but if FAILURE=0, NEWDAYS will be equal to closest (lower value) that correspond to a FAILURE=1 value in ONDAYS. For example in NEWDAYS observation 1 will equal to 59, but observation 6 will be equal to 66 (since the closest ordered "failure=1" value to 71 is 66). Can you please help me how to do this?
Thanks.
Roland