I am struggling with how to code values to paired cases. I need to assign the same pre-post periods of cases to one-to-one matched controls.

For example in the variables as follows: treat (0=untreated, 1= treated), pair_id: paired cases and controls grouped by the same id, time (0=before treatment, 1=after treatment)

I wanted to generate a variable time, where the values of time for untreated controls are the same as treated cases. The time for treated cases is already known.

I am wondering what command would allow me to generate the values in red.

Thank you for your help.


.
treat pair_id time
1 1 1
0 1 1
1 2 1
0 2 1
1 3 0
0 3 0
1 4 0
0 4 0
1 5 1
0 5 1