替换字符串变量
在Stata中,我试图用某种类型替换议会政党团体变量PPG,例如,如果政党名称此时属于该政党团体,则为EEP。问题似乎是字符串变量(partyname),因为每次我将名称复制到命令中时,Stata 似乎无法识别该值,并且总是显示“0 次更改”。我已经用 strtrim 修剪了变量,因此空白不可能是造成这种情况的原因。
之后:
replace partyname = strtrim(partyname)
tab partyname
codebook partyname, tab(1000)
我得到的政党名称每边都有两个“”“”,也许这就是问题所在?请参阅此处: 密码本结果
然后我继续(仅作为一方的示例):
gen PPG = "NA"
label var PPG "Parliamentary Party Groups of the EP"
replace PPG = "EEP-ED" if partyname == "Österreichische Volkspartei" & date < td(20jul2004)
并且Stata 然后说:“O 已进行更改。”
对此进行编码并不能解决问题,反而会使问题变得更加复杂。
In Stata, I am trying to replace a parliamentary party group variable PPG with a certain type, e.g., EEP if the party names belong to that party group at this time. The issue seems to be the string variable (the partyname), since every time I copy the name into the command, Stata does not seem to recognise the value and always states '0 changes made'. I have trimmed the variable with strtrim so blanks cannot be the reason for this.
After the following:
replace partyname = strtrim(partyname)
tab partyname
codebook partyname, tab(1000)
I get party names with two "" "" on each side, maybe that's the issue? See here: codebook result
I then proceed (just as an example with one party here):
gen PPG = "NA"
label var PPG "Parliamentary Party Groups of the EP"
replace PPG = "EEP-ED" if partyname == "Österreichische Volkspartei" & date < td(20jul2004)
and Stata then says: 'O changes made.'
Encoding this does not solve the issue but rather makes it more complicated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将测试该文本的出现,因此忽略之前和之后的任何字符。
修剪函数删除简单的空格,但它们不会删除以某种方式包含在字符串中的引号。
我在未来提到一个更令人费解的细节。它们不会删除某些看起来像空格的 ASCII 字符。 ASCII 160 就是一个示例。
请参阅 SSC 的
chartab
作为记录字符串变量中出现的字符的方法。would test for the occurrence of that text and so ignore any characters before and after.
The trim functions remove simple spaces, but they won't remove bounding quotation marks that somehow have been included in a string.
I mention for the future a more puzzling detail. They won't remove either certain ASCII characters that look like spaces. ASCII 160 is an example.
See
chartab
from SSC as a way of documenting what characters appear in your string variable.