在Stata中拆分字符串变量

huangapple go评论68阅读模式
英文:

Split string variable in Stata

问题

我有一个包含与公司信息相关的报告的唯一ID的数据库。我有一个包含在报告中涉及的公司的变量。例如,对于ID 1,我在公司列中有“公司A”,对于ID 2,我在该列中有“公司A; 公司B,公司C”。

我想要拆分公司变量,并生成与唯一报告中涉及的公司数量相同的行。因此,我想要对ID 2有三个不同的行,每行一个公司,其余变量保持相同。换句话说,我想要有一个面板数据,ID是公司,j变量是报告(对于某些公司可能相同)。

提前感谢。

英文:

I have a database with unique id for reports related with companies information. I have a variable which contains the companies involved in the report. For example, for ID 1, I have in the column companies “Compay A”, for ID 2 I have this column “Company A; Company B, Company C”.

I would like to split the variable companies and generate as many rows as companies involved in unique reports. So, I would like to have for the ID 2 three different rows, one for each, keeping the rest of variables equal. In other words, I would like to have a Panel Data, the ID is the company an the j variable the report (could be the same for some companies).

thanks in advance.

答案1

得分: 1

以下是翻译好的内容:

使用 `dataex` 提供一些示例数据始终是有帮助的。在下面的解决方案中,我已经这样做了。在加载示例数据之后,逐行运行代码以查看发生了什么。

* 由 -dataex- 生成的示例。要获取更多信息,请键入 help dataex
clear
input str3 id str12 companies
"id1" "C-A;C-B;C-C;"
"id2" "C-X;C-Y;C-Z;"
end

* 将字符串拆分为每家公司的一个变量
split companies, parse(";") generate(comp)

* 从宽格式转换为长格式
reshape long comp, i(id)

* 清理变量
drop _j companies

请注意,代码部分没有翻译。

英文:

It is always helpful to provide some example data using dataex. I have done so in the solution to your question below. After you load the example data, run one line at the time to see what is happning.

* Example generated by -dataex-. For more info, type help dataex
clear
input str3 id str12 companies
"id1" "C-A;C-B;C-C;"
"id2" "C-X;C-Y;C-Z;"
end

* Split the string into one var per company
split companies , parse(";") generate(comp)

* Reshape from wide to long
reshape long comp, i(id)

* Clean up variables
drop _j companies

huangapple
  • 本文由 发表于 2023年2月23日 21:26:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75545481.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定