全部版块 我的主页
论坛 计量经济学与统计论坛 五区 计量经济学与统计软件 Stata专版
20205 9
2011-03-24
大家好,在这里请教大家一个问题,希望大家能给予解答,非常感谢了!

是这样的,我现在要处理一组数据,需要生成新的变量,但这个变量比较特殊。这个变量需要辨别另一个变量中字符串中的短语。例如id==1 对应的变量x==“苹果,香蕉,橘子 李子 菠萝、哈密瓜”,那么我需要生成的新变量y就应该等于6。注意,各个短语间的标点符号由于在输入时造成了不一致。我的数据库样本大约30万,所以渴求简单方法呀!希望各位大侠帮帮忙告知个简单的stata语句吧!
二维码

扫码加我 拉你入群

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

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

全部回复
2011-3-24 13:34:57
这个好像得先分类,我不确定对不对
二维码

扫码加我 拉你入群

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

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

2011-3-24 14:46:04
foreach i in "," "、" /*枚举所有的非空格标点*/{
replace x=subinstr(x,"`i'"," ",.)
}
gen y=wordcount(x)
二维码

扫码加我 拉你入群

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

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

2011-3-26 11:19:09
非常感谢你,但是还是出现了一些错误,见下:
. foreach i in "," "、" /*枚举所有的非空格标点*/{
  2. replace x=subinstr(x,"`i'"," ",.)
  3. }

x ambiguous abbreviation
r(111);

我觉得是因为我的数据库中该变量的字符串中有x引起的
3# jzhyue
二维码

扫码加我 拉你入群

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

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

2011-3-26 11:27:47
3# jzhyue


我又验证了下,不是因为我刚刚说的那个原因,呵呵……
下面是错误的解释
error . . . . . . . . . . . . . . . . . . . . . . . .  Return code 111
        __________ not found;
        no variables defined;
        The variable does not exist.  You may have mistyped the
        variable's name.
        variables out of order;
        You specified a varlist containing varname1-varname2, yet
        varname1 occurs after varname2.  Reverse the order of the
        variables if you did not make some other typographical error.
        Remember, varname1-varname2 is taken by Stata to mean varname1,
        varname2, and all the variables in dataset order in between.
        Type describe to see the order of the variables in your dataset.
        __________ not found in using data;
        You specified a varlist with merge, yet the variables on which
        you wish to merge are not found in the using dataset, so the
        merge is not possible.
        __________ ambiguous abbreviation;
        You typed an ambiguous abbreviation for a variable in your data.
        The abbreviation could refer to more than one variable.  Use a
        nonambiguous abbreviation or, if you intend all the variables
        implied by the ambiguous abbreviation, append a `*' to the end
        of the abbreviation.
(end of search)



我觉得x在这里应当是字符串中的一个字符,所以这样直接定义为x会不会有误呀?期待您的指点!
二维码

扫码加我 拉你入群

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

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

2011-3-26 14:13:51
5# cheerhappy
这里面的x是你要处理的变量的名字,不是变量的值啊;错误的消息是说,数据中有多个变量的名字中有x字符,或者数据中没有变量名含有x字符
二维码

扫码加我 拉你入群

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

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

点击查看更多内容…
相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

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