楼主: lzlsnake
10952 9

[编程问题求助] subinstr 同时替换多个字符串 [推广有奖]

  • 0关注
  • 18粉丝

已卖:175份资源

讲师

5%

还不是VIP/贵宾

-

威望
0
论坛币
1177 个
通用积分
7.6120
学术水平
9 点
热心指数
14 点
信用等级
8 点
经验
234103 点
帖子
152
精华
0
在线时间
638 小时
注册时间
2010-3-5
最后登录
2024-4-2

楼主
lzlsnake 发表于 2015-3-9 23:15:13 |AI写论文
100论坛币
大家好,我的数据里面有乱七八糟的符号,数据如下:

Ia+mthe?bes&t+yo&uarethe?best?theya+ret&h?ebest

我用 subinstr 函数将他们都删掉,程序如下:

replace abc=subinstr(abc,"&","",.)
replace abc=subinstr(abc,"+","",.)
replace abc=subinstr(abc,"?","",.)


其实这样就OK了,但是我那个完美主义的老板要求我把这三个命令写在一个命令里,他不会写但他坚信必有一种方法可以搞定

悬赏100论坛币,多谢了。

最佳答案

hplcdadong 查看完整内容

Hope this is what your boss wants:
关键词:subinstr bins SUB bin 字符串 subinstr

本帖被以下文库推荐

沙发
hplcdadong 发表于 2015-3-9 23:15:14
Hope this is what your boss wants:

  1. foreach name in "&" "+" "?" {
  2. replace abc=subinstr(abc,"`name'","",.)
  3. }
复制代码
已有 2 人评分论坛币 学术水平 热心指数 信用等级 收起 理由
日新少年 + 1 + 1 + 1 精彩帖子
SpencerMeng + 20 + 1 + 1 精彩帖子

总评分: 论坛币 + 20  学术水平 + 2  热心指数 + 2  信用等级 + 1   查看全部评分

藤椅
lzlsnake 发表于 2015-3-10 20:18:00
hplcdadong 发表于 2015-3-10 06:29
Hope this is what your boss wants:
多谢!多谢!多谢!

板凳
lzlsnake 发表于 2015-3-10 21:18:40
hplcdadong 发表于 2015-3-10 06:29
Hope this is what your boss wants:
您的代码管用,但是我老板要的不止是三个符号,其实我们有三十多个符号要清理,如下:

"~" "`" "!" "@" "#" "$" "%" "^" "&" "*" "(" ")" "-" "_" "+" "=" "[" "]" "{" "}" "\" "|" ";" ":" "’" " " """ "." "," "<" ">" "?" "/"

当我把这些全部写进do file的时候,就出问题了,是不是因为太多了(我用的是Stata MP)?是不是应该先把这些符号都写进一个macro里面,然后在foreach的时候调用那个macro就好了?如何编写呢?

期待您的答复!

报纸
hplcdadong 发表于 2015-3-11 08:59:52
If you have many things to clean, you'd better use the following new user-written command:

  1. ssc install txttool, replace        //install the command
  2. txttool abc, gen(abc_new)       //abc_new should be what you want
复制代码
已有 1 人评分学术水平 热心指数 信用等级 收起 理由
日新少年 + 1 + 1 + 1 精彩帖子

总评分: 学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

地板
lzlsnake 发表于 2015-3-11 13:50:01
hplcdadong 发表于 2015-3-11 08:59
If you have many things to clean, you'd better use the following new user-written command:
哦哦,txttool,又学习了,多谢多谢!

不过我找到之前出错的原因了,并不是因为符号太多了,而是因为双引号,例如

原始字符串:china?gdp+growth&rate"is"high
我想要的字符串:chinagdpgrowthrateishigh

按照您的代码,是没有问题的
  1. foreach name in "&" "+" "?" {
  2. replace abc=subinstr(abc,"`name'","",.)
  3. }
复制代码
争对双引号,我去查了double quotation,写了下面这个代码
  1. foreach name in "&" "+" "?" `"""' {
  2. replace abc=subinstr(abc,"`name'","",.)
  3. }
复制代码
Stata出错
  1. too few quotes
  2. r(132)
复制代码

我觉得自己没写错啊,到底是哪里错了?期待您的答复。


已有 1 人评分学术水平 热心指数 信用等级 收起 理由
日新少年 + 1 + 1 + 1 精彩帖子

总评分: 学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

7
蓝色 发表于 2015-3-11 16:23:26
  1. clear
  2. input str60 x
  3. "Ia+mthe?bes&t+yo&uarethe?best?theya+ret&h?ebest"
  4. `"china?gdp+growth&rate"is"high"'
  5. end
  6. list


  7. foreach name in "&" "+" "?" `"""' {
  8.     replace x=subinstr(x,`"`name'"',"",.)
  9. }
  10. list
复制代码
  1. . clear

  2. . input str60 x

  3.                                                                 x
  4.   1. "Ia+mthe?bes&t+yo&uarethe?best?theya+ret&h?ebest"
  5.   2. `"china?gdp+growth&rate"is"high"'
  6.   3. end

  7. . list

  8.      +-------------------------------------------------+
  9.      |                                               x |
  10.      |-------------------------------------------------|
  11.   1. | Ia+mthe?bes&t+yo&uarethe?best?theya+ret&h?ebest |
  12.   2. |                   china?gdp+growth&rate"is"high |
  13.      +-------------------------------------------------+

  14. .
  15. .
  16. . foreach name in "&" "+" "?" `"""' {
  17.   2.     replace x=subinstr(x,`"`name'"',"",.)
  18.   3. }
  19. (2 real changes made)
  20. (2 real changes made)
  21. (2 real changes made)
  22. (1 real change made)

  23. . list

  24.      +---------------------------------------+
  25.      |                                     x |
  26.      |---------------------------------------|
  27.   1. | Iamthebestyouarethebesttheyarethebest |
  28.   2. |              chinagdpgrowthrateishigh |
  29.      +---------------------------------------+
复制代码


引号还需要多加一对

已有 2 人评分论坛币 学术水平 热心指数 信用等级 收起 理由
日新少年 + 1 + 1 + 1 精彩帖子
lzlsnake + 5 + 2 + 2 + 2 热心帮助其他会员

总评分: 论坛币 + 5  学术水平 + 3  热心指数 + 3  信用等级 + 3   查看全部评分

8
lzlsnake 发表于 2015-3-12 14:10:00
蓝色 发表于 2015-3-11 16:23
引号还需要多加一对
非常感谢!到现在,我已经学会了foreach、在不同情况下用不同的引号,但我还是没有完全掌握,例如下面这种情况:

  1. clear

  2. input str5 name
  3. "jack,"
  4. "jack."
  5. "jack`"
  6. `"jack'"'
  7. `"jack""'
  8. end
  9. list name

  10. foreach x in "," "." "`" {
  11. replace name=subinstr(name,"`x'","",.)
  12. }

  13. foreach y in `"'"' `"""' {
  14. replace name=subinstr(name,`"`y'"',"",.)
  15. }

  16. list name
复制代码


我只能写2个foreach来完成任务,必须为单引号和双引号额外写一个foreach,我试了很久,都无法把这2个foreach合在一起,总会报错。那么到底有没有办法可以写到一起呢?我知道这是吹毛求疵的要求了,不过还是很好奇。

期待您的答复,谢谢!

9
Val-GA 学生认证  发表于 2019-4-29 21:09:46
有用,谢谢!

10
日新少年 学生认证  发表于 2021-8-8 22:57:19
http://blog.sina.com.cn/s/blog_71e93c250100yd9s.html

replace name=subinstr(name," ","",.)

这个不错

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

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2025-12-29 08:41