全部版块 我的主页
论坛 计量经济学与统计论坛 五区 计量经济学与统计软件 Stata专版
2454 4
2012-12-09
现有一个类别变量industry,取值从1~99,每个取值分别代表不同的细分行业,并且已经添加了值标签。现在想建立一个行业大类的分类变量sector,如果按照普通的方法,应该是:
gen sector = 1 if industry == 1 | industry == 2 | industry == 5 | industry == 6
......

gen sector = 5 if industry == 78 | industry == 80 | industry == 81 | industry == 83
......

觉得这样好麻烦!
Stata11有没有什么命令可以让if后面的条件简化些,比如:
gen sector = 1 if 命令(varname, value1, value2, value3, value4,...)

即只要变量var的取值满足后面的多个取值, 就给变量sector赋值为1.
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2012-12-9 23:17:53
因为你的sector和industry对应并不规则,所以只好用foreach了

loc sector1 "1 2 5 6"
loc sector5 "78 80 81 83"
forv i=1/N{ /* N是你的sector总数
gen secot`i'=0
foreach v of loc sector`i'{
replace sector`i'=1 if industry==`v'
}
}


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2012-12-9 23:55:44
inrange(var,..............)

inlist(var,...........)
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2012-12-10 01:49:15
大白菜2012 发表于 2012-12-9 23:55
inrange(var,..............)

inlist(var,...........)
循着你的回复,在utilities中的[D]data management 第471页看到下面的例子:

We want to recode item by collapsing 1 and 2 into 1, 3 into 2, and 4 to 7 (boundaries included)
into 3.
. recode item (1 2 = 1) (3 = 2) (4/7 = 3), gen(Ritem)
or
. gen Ritem = item
. replace Ritem = 1 if inlist(item,1,2)
. replace Ritem = 2 if item==3
. replace Ritem = 3 if inrange(item,4,7)

多谢啦!
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2012-12-10 01:52:09
fgleric 发表于 2012-12-9 23:17
因为你的sector和industry对应并不规则,所以只好用foreach了

loc sector1 "1 2 5 6"
谢谢,学习了!
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群