从字符串响应中删除特定字符

Drop a specific character from string responses

我有一个字符串变量,一些响应的开头有一个额外的字符。在所有情况下,所讨论的字符都是常量字符。变量是 ICD 代码。例如,我用的不是 G23,而是 DG23。

在 Stata 中有没有办法去除多余的 D 字符?

我的数据是这样的

ID diag
1 DZ456
2 DG32
3 DY258
4 DD35
5 DS321
6 DD21
7 DA123

有关该地区的基本信息,请参阅help string functions

* Example generated by -dataex-. To install: ssc install dataex
clear
input byte d str5 diag
1 "DZ456"
2 "DG32" 
3 "DY258"
4 "DD35" 
5 "DS321"
6 "DD21" 
7 "DA123"
end

replace diag = substr(diag, 2, .) if substr(diag, 1, 1) == "D"

list 

     +----------+
     | d   diag |
     |----------|
  1. | 1   Z456 |
  2. | 2    G32 |
  3. | 3   Y258 |
  4. | 4    D35 |
  5. | 5   S321 |
     |----------|
  6. | 6    D21 |
  7. | 7   A123 |
     +----------+

字符串函数的替代方法是使用正则表达式,请参阅 help regex

replace diag = regexs(1) if regexm(diag, "^D(.*)")