生成虚拟变量

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

我的 Stata 有问题。

我有两个变量:

country
country_birthplace

例如:

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

我想创建一个虚拟变量,如果

country
country_birthplace
都是
"US"
,则该变量为 0; 1 在所有其他情况下;除非
country_birthplace
中有缺失值,当我想要缺失值时。

我尝试了这段代码:

experience_abroad = cond(missing(country_birthplace), ., (country == "US" & country_birthplace == "US" ? 0 : 1))

但这给了我相反的虚拟变量。因此,对于

US
US
,我得到 1,在所有其他情况下得到 0。

怎样才能得到我想要的输出?

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.