楼主: cheerhappy
18953 9

[其他] 如何在stata中识别字符串 [推广有奖]

  • 3关注
  • 7粉丝

讲师

22%

还不是VIP/贵宾

-

威望
0
论坛币
290 个
通用积分
0.0690
学术水平
6 点
热心指数
6 点
信用等级
7 点
经验
5316 点
帖子
373
精华
0
在线时间
241 小时
注册时间
2010-3-18
最后登录
2023-3-27

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
大家好,在这里请教大家一个问题,希望大家能给予解答,非常感谢了!

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

扫码加我 拉你入群

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

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

关键词:Stata tata 字符串 请教大家一个问题 STATA语句 标点符号 简单方法 字符串 哈密瓜 数据库

沙发
gongxiaobo2006 发表于 2011-3-24 13:34:57 |只看作者 |坛友微信交流群
这个好像得先分类,我不确定对不对
我是吉林大学商学院数经的,Q564778089
人人id 290226950

使用道具

藤椅
jzhyue 发表于 2011-3-24 14:46:04 |只看作者 |坛友微信交流群
foreach i in "," "、" /*枚举所有的非空格标点*/{
replace x=subinstr(x,"`i'"," ",.)
}
gen y=wordcount(x)

使用道具

板凳
cheerhappy 发表于 2011-3-26 11:19:09 |只看作者 |坛友微信交流群
非常感谢你,但是还是出现了一些错误,见下:
. foreach i in "," "、" /*枚举所有的非空格标点*/{
  2. replace x=subinstr(x,"`i'"," ",.)
  3. }

x ambiguous abbreviation
r(111);

我觉得是因为我的数据库中该变量的字符串中有x引起的
3# jzhyue
不放弃、不抛弃

使用道具

报纸
cheerhappy 发表于 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会不会有误呀?期待您的指点!
不放弃、不抛弃

使用道具

地板
houquan 发表于 2011-3-26 14:13:51 |只看作者 |坛友微信交流群
5# cheerhappy
这里面的x是你要处理的变量的名字,不是变量的值啊;错误的消息是说,数据中有多个变量的名字中有x字符,或者数据中没有变量名含有x字符
We all love to instruct, though we can teach only what is not worth knowing. -- J. Austen

使用道具

7
cheerhappy 发表于 2011-3-26 16:56:38 |只看作者 |坛友微信交流群
6# houquan


这样的话我该怎么处理呀?
不放弃、不抛弃

使用道具

8
jzhyue 发表于 2011-3-27 17:25:32 |只看作者 |坛友微信交流群
cheerhappy 发表于 2011-3-26 11:27
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会不会有误呀?期待您的指点!
提示说明x缩略无效,你可以交将x所代表的缩略变量全名写出来(最少要缩略明确)而不要用x表示。

使用道具

9
cheerhappy 发表于 2011-4-1 20:02:07 |只看作者 |坛友微信交流群
8# jzhyue

. foreach i in "," "、" /*枚举所有的非空格标点*/{
  2. replace zdbz=subinstr(zdbz,"`i'"," ",.)
  3. }

. foreach i in "," "、" /*枚举所有的非空格标点*/{
  2. replace x=subinstr(zdbz,"`i'"," ",.)
  3. }


上面两种形式,您说的是第二个吗?

我不太懂您说的意思,所以还是得咨询您,呵呵……
不放弃、不抛弃

使用道具

10
jzhyue 发表于 2011-4-2 08:20:39 |只看作者 |坛友微信交流群
你将数据sample列出来,让大家可以有目的的来解决问题。

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-5-21 07:41