Reputation: 372
I have some hand corrections to do in a data set and I wanted to do avoid repeating the if
condition. For instance, instead of
sysuse auto, clear
replace mpg=21 if make=="AMC Concord"
replace rep78=4 if make=="AMC Concord"
replace mpg=23 if make=="AMC Pacer"
replace rep78=4.5 if make=="AMC Pacer"
the following code would be more parsimonious but it doesn't work
sysuse auto, clear
replace mpg=21 & rep78=4 if make=="AMC Concord"
replace mpg=23 & rep78=5 if make=="AMC Pacer"
Any suggestion would be helpful.
Upvotes: 1
Views: 6727
Reputation: 37318
You are right; that code would be more concise. It just is not legal Stata and nothing in the syntax diagram for replace
suggests otherwise.
I can imagine some very contrived ways of re-writing the same code in fewer lines, while using if
, but none would be, in my view, more attractive or easier to understand.
Looking up the observation number and using in
rather than if
would also shorten the code, but be less clear and highly fragile to changes in sort
order.
Upvotes: 1