全部版块 我的主页
论坛 计量经济学与统计论坛 五区 计量经济学与统计软件 Stata专版
30525 5
2019-03-31
各位大神,本人在使用merge横向合并两个数据表时,出现如下错误“key variable Code is long in master but str19 in using data
Each key variable -- the variables on which observations are matched -- must be of the same generic type in the master and using datasets.  Same generic type means both numeric or both string.”使用的代码如下:
复制代码
主要系target数据表中关键变量Code(股票代码)在进行数据导入时变成numeric类型,Code每个样本前的0被自动删除,比如000001直接变成了1,而使用数据表Dt1中的Code是文本类型。这样合并的时候就出现了上述问题。请问如何将target数据表中的变量Code样本前补充0,变成6位的股票代码,变成文本类型,这个如何处理?或者有其他更好的方式?谢谢!
target数据
复制代码
dt1数据
复制代码






二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2019-3-31 15:58:06
你的 dt1 资料要先
复制代码
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2019-4-7 16:45:55
黃河泉 发表于 2019-3-31 15:58
你的 dt1 资料要先
醍醐灌顶,谢谢黄老师
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2019-4-7 17:08:22
姜先生 发表于 2019-4-7 16:45
醍醐灌顶,谢谢黄老师
没那么严重吧!呵呵!
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2023-6-21 11:37:01
https://www.statalist.org/forums ... ble-types-different
The contents of a variable can be stored as either a numeric variable, one where Stata knows to interpret that content as a number, or as simply a "string" of alpabetic or numeric characters, without paying attention to whether it is numeric or not. This is true even for strings of numerals, such as "19432165." In a CSV file, if that string of numbers is enclosed in quotes, Stata will read and interpret it as simply a string of characters *even if those characters are numerals,* and store it as a string variable and not as a numeric variable. The way in which a variable is stored and interpreted is known in computer programming as its "data type." Variables with different data types cannot be matched or compared unless they are made to be of the same type. This issue is not unique to Stata, but in fact is present in all programs, but possibly hidden from the user.

Stata is telling you that in one file, Stata has stored the key variable as a string, and in the other as a numeric variable of type long. (long is a numeric type variable that holds only integers, but which can hold very large "long" ones. See -help datatype-.) It might be that before now, the variable rifisid had always been put into the CSV file as a string, but someone now prepared that file with rifisid as a numeric variable. That sort of mistake is pretty common in the data processing world.
Stata provides functions to create a numeric variable from a string variable containing numerals. See -help destring-. You can take your "using" file, and convert the string version of rifisid to a long variable, save the file, and then do the merge.
Code:
use "Tidy675_2021.dta"
destring rifisid, gen(temp)
rename rifisid rifisid_as_string // keep copy just in case
rename temp rifisid
recast long rifisid
save "Tidy675_2021.dta"
clear
use "YourMasterFile.dta"
merge m:1 rifisid ....
Dates are a specific and complicated case here, where there are many different ways to use a string to represent a date, and many different ways to store the numeric information of a date. That's a difficult topic, best left aside for the moment.
Last edited by Mike Lacy; 27 May 2021, 15:20. Reason: Forgot to illustrate "clear"
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2024-9-11 10:38:14
在 Stata 中处理这种数据类型不匹配的问题时,你可能需要先转换你的 `Code` 变量类型以确保它与你想要合并的数据集中的变量类型一致。

如果你的目标是让 `Code` 成为字符串类型(因为股票代码通常包含前导零),你可以使用以下命令将数值类型的 `Code` 转换为字符串:

```stata
encode Code, gen(strCode)
destring strCode, replace
```

但是,更简单的方法是直接转换数字型的 `Code` 为字符串,并确保保留前导零。这可以通过 `format` 或者 `discrete` 格式来实现。假设你的股票代码最大长度可能为6位(例如000001),你可以这样操作:

```stata
gen strCode = sprintf("%06d", Code)
```

或者使用更简单的转换方法,直接将数字型的 `Code` 转换为字符串类型:

```stata
destring Code, replace
```

然后你可能需要重新设置格式以确保前导零被保留。如果 `Code` 最大长度是6位,可以这样做:

```stata
format strCode %06.0g
```

但是,更好的做法是在读取数据时就直接将其设定为字符串类型。在导入数据文件(如CSV或Excel)时使用正确的命令选项将变量指定为字符串类型。

例如,如果你正在从 CSV 文件中导入数据:

```stata
import delimited "path_to_file.csv", varnames(1) clear strL(Code)
```

这样可以避免后续的数据转换步骤。之后你就可以进行合并操作了,确保两个数据集中的 `Code` 和其他关键变量类型一致:

```stata
merge 1:1 Code year using "C:\Users\jzz\OneDrive\小论文\小论文\Dt1.dta", update
```

以上方法应该可以帮助解决你的问题。

此文本由CAIE学术大模型生成,添加下方二维码,优先体验功能试用



二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群