Hi all,

I have a question regarding random selection.

I want to create a new variable Z, which is chosen from the existing variable C. The condition is that the range for the selection of A is with the same D variable. However, the C variable is dummy 0 and 1.

For example, the data is:

ID A B C D
1 0 0.1 0 1
2 9 0.3 0 3
3 8 0.4 1 2
4 10 0.6 1 2
5 10 0.8 0 1
6 8 0.3 1 1
7 10 0.3 0 3
8 6 0.5 1 3


For ID=1, Z variable will be selected from the same D variable, which is from ID=5 or ID=6. Therefore, either the C value of the 5th or 6th observation will be the final value for the Z variable. The selection is totally random.

Please help me out of the Stata codes.
Thanks in advance