【问题标题】:reshape wide to long in stata but new variable contains all missing values在 stata 中将宽整形为长,但新变量包含所有缺失值
【发布时间】:2017-01-30 14:24:46
【问题描述】:

我需要reshape一个数据集,其原始形式如下:

schid     m2s1q0_i     m2s1q0_ii    ...     m2s1q0_x
1            6            2                   3

我想reshape把它变成长格式,像这样:

schid teacher_id
1        5
1        2
...      
1       3

我使用了这个代码:

reshape long m2s1q0_, i(schoolid) j(teacher_id)

但是,teacher_id 变量全部丢失。哪里出错了?

【问题讨论】:

  • help reshape,有一个选项字符串

标签: stata reshape


【解决方案1】:

如果您使用选项string,教师 ID 将作为字符串变量生成,而不是丢失。然后您可以使用encode 为teacher_id 变量创建数值

这是一个例子:

clear
set obs 10
gen schid = _n
gen m_i = 1
gen m_ii = 2
gen m_iii = 3
reshape long m_, i(schid) j(teacher_id) string
encode teacher_id, gen(teacher_id2)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-26
    相关资源
    最近更新 更多