Loops 将一个变量的值替换为其他变量的值Stata 13
我第一次和斯塔塔比赛。直到本周,我才使用过它,我正在尝试通过一些例子来学习。我有以下一组数据:Loops 将一个变量的值替换为其他变量的值Stata 13,loops,for-loop,foreach,stata,Loops,For Loop,Foreach,Stata,我第一次和斯塔塔比赛。直到本周,我才使用过它,我正在尝试通过一些例子来学习。我有以下一组数据: contruse | educ_none | educ_prim | educ_secabove 1 | 0 | 1 | 0 0 | 1 | 0 | 0 ... 我用相应的数据集创建了以下变量,这样我就可以对所有不同的教育进行控制 gen education=0 replace education=1 if
contruse | educ_none | educ_prim | educ_secabove
1 | 0 | 1 | 0
0 | 1 | 0 | 0
...
我用相应的数据集创建了以下变量,这样我就可以对所有不同的教育进行控制
gen education=0
replace education=1 if educ_none==1
replace education=2 if educ_prim==1
replace education=3 if educ_secabove==1
replace education=. if educ_none==. | educ_prim==. | educ_secabove==.
tab education, missing
contruse | educ_none | educ_prim | educ_secabove | education
1 | 0 | 1 | 0 | 2
0 | 1 | 0 | 0 | 1
基本上,有没有更好的方法来做到这一点:例如,我的varlist可以任意大,而执行上述操作是痛苦的。有没有一种方法可以说,将以下内容颠倒过来,以处理多个变量并为单个变量指定一个值
foreach x of varlist educ_none educ_prim educ_secabove {
replace `x' = . if var > 3
}
你能使这个过程自动化吗?答案是“否”,因为每个组件变量都有一个唯一的后缀。因此,如果你有“种族黑人”“种族hisp\u nonw”“种族白人”,你就不能以同样的方式处理“教育”和“种族”变量。您还将为每个变量指定唯一的值标签参见下面的第二个答案 另外两个问题:
set linesize 100
clear
input id educ_none educ_prim educ_secabove
1 0 1 0
2 1 0 0
3 0 0 1
4 . 1 . /* Okay */
5 . . . /* Really Missing */
6 0 0 0 /* Really Missing */
7 . 1 1 /* Illegal */
end
egen etot = rowtotal(educ_*) /* = 1 for valid values */
foreach x of varlist educ_* {
/* Tentatively fix incorrect missings */
replace `x'= 0 if `x'==. & etot==1
}
list
gen education = 1 if educ_none==1
replace education=2 if educ_prim==1
replace education=3 if educ_secabove==1
/* Assign extended missing for illegal values*/
replace education = .a if etot >1 & etot<.
#delim ;
label define educl
1 "None"
2 "Primary"
3 "Secondary+"
.a ">1 indicator is 1"
;
#delim cr
label values education educl
list
tab education, missing
设置行大小100
清楚的
输入id educ_none educ_prim educ_Sec上文
1 0 1 0
2 1 0 0
3 0 0 1
4.1 . /* 好的*/
5 . . . /* 真想念*/
6 0 0/*真的不见了*/
7.1/*非法*/
结束
对于有效值,egen etot=rowtotal(educ_*)/*=1*/
varlist educ的foreach x{
/*暂时修复错误的缺失*/
如果'x'=.&etot==1,则替换'x'=0
}
列表
如果教育程度=1,则gen education=1
如果educ_prim==1,则替换education=2
如果上述教育=1,则替换教育=3
/*为非法值分配扩展缺失*/
替换教育=.a如果etot>1&etot除了Steve Samuels的优秀建议外,该地区还有三种标准设备:
A.使用重新编码
。看看它的帮助
B
(当且仅当最多一个指示器为1时,此选项才有效)
C
注:
C1。上面的代码是一条语句。布局只是为了帮助使结构可见
C2。就像在初等代数中一样,每个左括号(
意味着承诺用右括号匹配它)
。嵌套调用cond()
不会改变这一点
C3。有关cond()的更多信息,请访问2014-06-02自动进近
在声明创建和标记新变量的过程不能自动化之后,我决定尝试一下。我在SSC上找到了两个有用的命令:罗杰·纽森的varlabdef和丹尼尔·克莱因的labvalch3。两者都可以从Stata内部下载,例如ssc安装varlabdef
我假设,与原始示例一样,每个0-1变量名的形式为“root_后缀”,并且具有相同根的变量中正好有一个值为1。目标是为每个根创建一个新变量,其值对应于值为1的指示符变量(如果有)的顺序。用户首先创建一个包含所有根的本地宏。程序通过根循环,每个过程中创建一个变量;一个内部循环实现了Nick的解(B)varlabdef根据原始指标的名称创建值标签;和labvalch3除去后缀之外的所有内容,并将每个项目大写。然后使用label values
语句将此值标签指定给新变量。在循环外部,新变量被赋予带有标签variable
的变量标签
在下面的示例中,有两个“根”educ
和gender
。例如,根为“性别”的变量是gender\u male
和gender\u male
。初始化一个新变量gender
,然后为男性分配值1,为女性分配值2。相应的值标签(也称为“性别”)被定义并与新变量关联,变量本身被标记为“性别”
gen education = educ_none + 2 * educ_prim + 3 * educ_secabove
gen education = cond(educ_secabove == 1, 3,
cond(educ_prim == 1, 2,
cond(educ_none == 1, 1)))
clear
input id educ_none educ_prim educ_secabove gender_male gender_female
1 0 1 0 1 0
2 1 0 0 1 0
3 0 0 1 0 1
4 0 1 0 1 0
end
/* Create local macro to hold root names */
local roots educ gender
/* Loop over each root */
foreach v of local roots {
qui gen `v' = 0 /* Initialize new variable from root */
/* Get number of component variables */
qui ds `v'_*
local wc : word count `r(varlist)'
/* Create new variables */
forvalues k = 1/`wc' {
/* z`k' is the k-th component variable */
local z`k' : word `k' of `r(varlist)' /* extended macro */
qui replace `v' = `v'+`k'*`z`k''
}
/* Total components to check for missing/illegal values*/
egen `v'tot = rowtotal(`v'_*)
replace `v' = . if `v'tot != 1
replace `v' = .a if `v'tot>1 & `v'tot<.
/* Create value labels from variable names. Note that
value labels can have same names as the
the variables they label*/
/* Create a value label consisting of the component variable names */
varlabdef `v', vlist(`v'_*) from(name)
label define `v' .a "Illegal", add
/* Remove the roots from the labels and capitalize */
labvalch3 `v', subst("`v'_" "")
labvalch3 `v', strfcn(proper("@"))
/* Assign the value labels to the new variables */
label values `v' `v'
}
/* Give nice labels to the new variables */
label var educ "Education"
label var gender "Gender"
label list
tab educ
tab gender
. label list
gender:
1 Male
2 Female
.a Illegal
educ:
1 None
2 Prim
3 Secabove
.a Illegal
. tab educ
Education | Freq. Percent Cum.
------------+-----------------------------------
None | 1 25.00 25.00
Prim | 2 50.00 75.00
Secabove | 1 25.00 100.00
------------+-----------------------------------
Total | 4 100.00
. tab gender
Gender | Freq. Percent Cum.
------------+-----------------------------------
Male | 3 75.00 75.00
Female | 1 25.00 100.00
------------+-----------------------------------
Total | 4 100.00