给定以下数据集和命令:
sysuse auto, clear
generate x = .
replace x = 5 if price == 4099
replace x = 5 if price == 4749
我想生成一个新变量x
等于5
if price
属于值列表。
以下命令不会生成新值x
并且是不正确的:
replace x = 5 if price == 4099 & price == 4749
我需要一种更简单的单行方法,不涉及替换x
我在上面的代码中做到了这一点,它有效,但乏味且不优雅。
您需要使用“或”(|
) 而不是“和”(&
):
sysuse auto, clear
generate x = .
replace x = 5 if price == 4099
replace x = 5 if price == 4749
generate y = 5 if price == 4099 | price == 4749
或者,您可以使用inlist()
功能:
generate z = 5 if inlist(price, 4099, 4749)
Results:
list price x y z in 1 / 5
+-------------------+
| price x y z |
|-------------------|
1. | 4,099 5 5 5 |
2. | 4,749 5 5 5 |
3. | 3,799 . . . |
4. | 4,816 . . . |
5. | 7,827 . . . |
+-------------------+
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)