如何制作以下数据、

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

How to make the following dta、

问题

原始数据中有一个字符串变量"country"

country
US
US
UK
US
Canada
UK
US
Canada

为了减小文件大小,我想将其转换为数值变量,类似于

country
1
1
2
1
3
2
1
3

同时还要单独保存一个名为correspondence.dta的文件,内容如下:

数值

country
1 US
2 UK
3 Canada

要将其转换为数值以减小文件大小,可以执行以下操作:

encode country, gen(country2)
drop country
gen country3=country2
drop country2
rename country3 country

但由于我使用了country3,而不是具有字符串标签的country2,因此现在数据不包含我需要的字符串信息,尽管文件大小已成功减小。我需要那个correspondence.dta文件。如何在同时减小文件大小的情况下生成它?

英文:

Original data has a string variable "country"

country
US
US
UK
US
Canada
UK
US
Canada

To reduce file size, I want to turn this into a numeric variable, something like

country
1
1
2
1
3
2
1
3

while also separately saving a file correspondence.dta that looks like
numeric

country
1 US
2 UK
3 Canada

Changing it to numeric to reduce the file size could be done by doing this.

encode country, gen(country2)
drop country
gen country3=country2
drop country2
rename country3 country

But since I am using country3, but not country2 (which has string label), this data now do not contain string information that I need although file size has been successfully reduced.
I need that correspondence.dta file.
How can I generate that while also simultaneously reducing file size like this?

答案1

得分: 1

你可以使用label save country2 using country_labels.do保存值标签。这将创建一个可用于重新创建您的值标签的 do 文件。如果您继续在 Stata 中工作,这就足够了。

如果你真的想将它存储为第二个 .dta 文件,可以按照以下方式修改 do 文件。在 do 文件编辑器中,选择编辑->查找->替换...,然后在查找字段中输入label define country2 ,将替换字段留空,然后点击全部替换。然后,对, modify做同样的操作。然后在 do 文件的剩余部分之前添加以下行

//如果一些国家名字超过50个字符,请将str50更改为更大的数字
输入ID str50国家

并将这些行添加到末尾

保存 correspondence.dta,替换
英文:

You can save the value labels with label save country2 using country_labels.do. This will create a do-file that can be used to re-create your value label. This is sufficient if you continue working in Stata.

If you really want to store it as a second .dta you can modify the do file as follows. In the do-file editor go on Edit->Find->Replace... then type label define country2 into the find field and leave replace empty, then hit replace all. After that, do the same with , modify. Then add the following lines above the remaining part of the do file

clear
//change str50 to a bigger number if some country names are longer than 50 characters
input ID str50 country

And these lines at the end

end
save correspondence.dta, replace

huangapple
  • 本文由 发表于 2023年6月12日 20:07:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76456527.html
匿名

发表评论

匿名网友

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

确定