Stata Wide to Long Reshape - 将宽变量标签应用为长值标签
Stata Wide to Long Reshape - Apply wide variable labels as long value labels
我有宽数据,其中 ID 是唯一标识符,还有一系列变量 branch1 - branch3
我想从宽数据转换。来自广泛数据的变量标签,然后需要应用为我认为的值标签,所以我可以保留分支名称。
clear
input id branch1 branch2 branch3
1 0 1 1
2 1 1 1
3 0 0 0
4 1 0 1
5 0 1 0
end
* Labels are already come with the data file, just creating some for example
label variable branch1 "Branch Name A"
label variable branch2 "Branch Name B"
label variable branch3 "Branch Name C"
* Reshape to long
reshape long branch, i(id) j(brn_name)
从这里我希望期望的输出是:
id brn_name branch branch_value
1 1 0 Branch Name A
1 2 1 Branch Name B
1 3 1 Branch Name C
2 1 1 Branch Name A
2 2 1 Branch Name B
2 3 1 Branch Name C
3 1 0 Branch Name A
3 2 0 Branch Name B
3 3 0 Branch Name C
4 1 1 Branch Name A
4 2 0 Branch Name B
4 3 1 Branch Name C
5 1 0 Branch Name A
5 2 1 Branch Name B
5 3 0 Branch Name C
由于您想要的变量 branch_value
基本上是 brn_name
值的标签,您可以利用 Stata 的功能来获得带有值标签的数字变量。
如果您不执行最后两行,下面的代码将执行此操作。否则,执行最后两行得到你要求的结果。
clear
input id branch1 branch2 branch3
1 0 1 1
2 1 1 1
3 0 0 0
4 1 0 1
5 0 1 0
end
* Labels are already come with the data file, just creating some for example
label variable branch1 "Branch Name A"
label variable branch2 "Branch Name B"
label variable branch3 "Branch Name C"
// Build value label
foreach var of varlist branch* {
local branch_nr: subinstr local var "branch" ""
label define branches_label `branch_nr' "`:variable label `var''", add
}
// Reshape
reshape long branch, i(id) j(brn_name)
// Assign label to brn_name
label values brn_name branches_label
// Possibly stop here!
// Decode value labels
decode brn_name, gen(branch_value)
label values brn_name .
我有宽数据,其中 ID 是唯一标识符,还有一系列变量 branch1 - branch3
我想从宽数据转换。来自广泛数据的变量标签,然后需要应用为我认为的值标签,所以我可以保留分支名称。
clear
input id branch1 branch2 branch3
1 0 1 1
2 1 1 1
3 0 0 0
4 1 0 1
5 0 1 0
end
* Labels are already come with the data file, just creating some for example
label variable branch1 "Branch Name A"
label variable branch2 "Branch Name B"
label variable branch3 "Branch Name C"
* Reshape to long
reshape long branch, i(id) j(brn_name)
从这里我希望期望的输出是:
id brn_name branch branch_value
1 1 0 Branch Name A
1 2 1 Branch Name B
1 3 1 Branch Name C
2 1 1 Branch Name A
2 2 1 Branch Name B
2 3 1 Branch Name C
3 1 0 Branch Name A
3 2 0 Branch Name B
3 3 0 Branch Name C
4 1 1 Branch Name A
4 2 0 Branch Name B
4 3 1 Branch Name C
5 1 0 Branch Name A
5 2 1 Branch Name B
5 3 0 Branch Name C
由于您想要的变量 branch_value
基本上是 brn_name
值的标签,您可以利用 Stata 的功能来获得带有值标签的数字变量。
如果您不执行最后两行,下面的代码将执行此操作。否则,执行最后两行得到你要求的结果。
clear
input id branch1 branch2 branch3
1 0 1 1
2 1 1 1
3 0 0 0
4 1 0 1
5 0 1 0
end
* Labels are already come with the data file, just creating some for example
label variable branch1 "Branch Name A"
label variable branch2 "Branch Name B"
label variable branch3 "Branch Name C"
// Build value label
foreach var of varlist branch* {
local branch_nr: subinstr local var "branch" ""
label define branches_label `branch_nr' "`:variable label `var''", add
}
// Reshape
reshape long branch, i(id) j(brn_name)
// Assign label to brn_name
label values brn_name branches_label
// Possibly stop here!
// Decode value labels
decode brn_name, gen(branch_value)
label values brn_name .