I'm confused about the difference between using if to group several statements and using if at the end of each statement. It seems to me they should do the same thing, but they don't.
For example, sometimes I want to replace the values of several variables in a subset of data defined by some variable. It seems to me I should be able to do this:
Which is elegant but doesn't change the values of mpg and displacement at all. Why not?
Instead, it seems I have to do this:
Which is effective but less readable, and would be a pain in the neck if there were more replace statements or a more complicated condition than if foreign==1.
What's the story here? Why doesn't the first solution do what I expect, and does it do anything at all?
For example, sometimes I want to replace the values of several variables in a subset of data defined by some variable. It seems to me I should be able to do this:
Code:
sysuse auto, clear if foreign==1 { replace mpg = 0 replace displacement = 0 } tabstat mpg displacement, by(foreign)
Instead, it seems I have to do this:
Code:
replace mpg = 0 if foreign==1 replace displacement = 0 if foreign==1 tabstat mpg displacement, by(foreign)
What's the story here? Why doesn't the first solution do what I expect, and does it do anything at all?
Comment