Stata 生成虚拟变量

问题描述 投票:0回答:1

我的 Stata 有问题。

我有两个变量:国家和国家出生地

例如:

国家 国家_出生地 虚拟变量
美国 美国 0
印度 印度 1
印度 美国 1
美国 (缺失值) 。 (缺失值)

我想创建虚拟变量,如果country和country_birthplace都是“美国”,则给出0,但在所有其他情况下给出1!除非 Country_birthplace 中存在缺失值,否则我希望虚拟变量中存在缺失值。

我尝试了这段代码: experience_abroad = cond(missing(country_birthplace), ., (country == "US" & Country_birthplace == "US" ? 0 : 1))

但这给了我相反的虚拟变量。所以我在美国和美国得到 1,在所有其他情况下得到 0。

如何正确更改 0 和 1 的输出,以便在 US&US 中得到 0,在所有其他情况下得到 1。当“country_birthplace”缺失时,还会缺失值吗?

conditional-statements stata
1个回答
0
投票

我对您的代码被接受为合法的暗示感到惊讶。

这是完全非法的:

  1. 您需要

    generate
    或其任何缩写。

  2. Mata 中允许使用

    x ? a : b
    语法,但 Stata 中不允许。

gen experience_abroad = cond(missing(country_birthplace), ., !(country == "US" & country_birthplace == "US")

应该有效,应该有效

gen experience_abroad = !(country == "US" & country_birthplace == "US") if !missing(country_birthplace) 

gen experience_abroad = cond(country == "US" & country_birthplace == "US", 0, 1) if !missing(country_birthplace) 
© www.soinside.com 2019 - 2024. All rights reserved.