Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Stata宽到长整形-应用宽变量标签作为长值标签_Stata - Fatal编程技术网

Stata宽到长整形-应用宽变量标签作为长值标签

Stata宽到长整形-应用宽变量标签作为长值标签,stata,Stata,我有广域数据,其中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 l

我有广域数据,其中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_值基本上是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 .

由于所需的变量branch_值基本上是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 .