Stata Replace Command Frustration

#1
I am using the replace command in a do file. When I run the do file, it looks like this (yes, I study diarrhea):

. replace cause_diar_2 = 1 in 20
(1 real change made)

. replace cause_diar_2 = 0 in 125
(0 real changes made)

. replace cause_diar_2 = 0 in 134
(0 real changes made)

. replace cause_diar_2 = 1 in 149
(1 real change made)

No error messages appear when I run the do file. Then, when I repeat the commands for the replacements that for some reason were not made, the changes are made (for example, the two in the middle above):

. replace cause_diar_2 = 0 in 125
(1 real change made)

. replace cause_diar_2 = 0 in 134
(1 real change made)

This must be stupidly simple to fix, but I am relatively new to stata. Help?

Thanks! Laura
 

bukharin

RoboStataRaptor
#2
I would strongly advise you not to do this. This command:
replace cause_diar_2 = 0 in 125
will make cause_diar_2 0 in the 125th observation in your dataset. But what is the 125th observation? If your dataset changes in any way - eg it gets sorted differently, observations are dropped, observations are added, the data are reshaped etc - you will be replacing cause_diar_2 in the wrong observation! You should almost never directly reference an observation in a do-file like this. Probably what's happening is that the order of observations is different the 2 times you're running the replace commands.

Instead you should be referencing the observation by characteristics that make it unique in your dataset. For example (totally made up):
replace cause_diar_2 = 0 if ID==889 & visit==1