楼主: guanren
3367 8

[统计软件与数据分析] 怎么把stata11中的字符型的数值,改为数值型的? [推广有奖]

  • 2关注
  • 0粉丝

已卖:16份资源

博士生

88%

还不是VIP/贵宾

-

威望
0
论坛币
1709 个
通用积分
2.0187
学术水平
3 点
热心指数
5 点
信用等级
3 点
经验
5597 点
帖子
152
精华
0
在线时间
532 小时
注册时间
2010-3-3
最后登录
2024-8-30

楼主
guanren 发表于 2017-7-31 11:50:26 |AI写论文
10论坛币
这是stata11的数据,读入之后,怎么都没法变成数值型,不管是用real,还是 destring,都提示有nonnu的数据,但是我检查了,没有。用real, 后面加了force。结果全部变成...了。求助。

股票代码.dta
下载链接: https://bbs.pinggu.org/a-2296923.html

51.07 MB

stata11的

关键词:Stata11 Stata tata 数值型 字符型

回帖推荐

Libaoxinxin 发表于4楼  查看完整内容

The most common cause of this error message is that you are trying to use a string variable with a command that only supports numeric variables. You can only tell the type of a variable by using the describe command. This is easy to fix. If you have a string variable and want to convert it to a numeric variable, you can use the encode command. If you have a string variable that has only number ...

沙发
bangbang88 发表于 2017-7-31 13:42:39
destring, replace
这个试过了吗?

藤椅
guanren 发表于 2017-7-31 13:53:47
试过了,还是提示有非数字

板凳
Libaoxinxin 发表于 2017-7-31 19:43:45
The most common cause of this error message is that you are trying to use a string variable with a command that only supports numeric variables. You can only tell the type of a variable by using the describe command.
This is easy to fix.
If you have a string variable and want to convert it to a numeric variable, you can use the encode command. If you have a string variable that has only numbers in it, then you can alternatively use the real() function.
. describe
   
Contains data
   obs:             4                          
  vars:             2                          
  size:            48
------------------------------------------------------------------------
                      storage  display     value
variable name   type   format      label      variable label
------------------------------------------------------------------------
a                    str4    %9s                  
b                    str4   %9s                    
------------------------------------------------------------------------
Sorted by:  
      Note:  dataset has changed since last saved

. list

      +-------+
      | a   b |
      |-------|
   1. | 1   a |
   2. | 2   b |
   3. | 3   c |
   4. | 4   d |
      +-------+

. gen na = real(a)
. encode b, gen(nb)
. describe
        
Contains data
   obs:             4                          
  vars:             4                          
  size:            80
------------------------------------------------------------------------
                   storage  display       value
variable name   type   format      label      variable label
------------------------------------------------------------------------
a               str4   %9s                    
b               str4   %9s                    
na              float  %9.0g                  
nb              long   %8.0g       nb         
------------------------------------------------------------------------
Sorted by:  
      Note:  dataset has changed since last saved

. list
      +-----------------+
      | a   b   na   nb |
      |-----------------|
   1. | 1   a    1    a |
   2. | 2   b    2    b |
   3. | 3   c    3    c |
   4. | 4   d    4    d |
      +-----------------+
Although nb is a numeric variable, it looks like a string variable because the encode command added value labels to it.
. list nb, nolab
      +----+
      | nb |
      |----|
   1. |  1 |
   2. |  2 |
   3. |  3 |
   4. |  4 |
      +----+
已有 1 人评分经验 论坛币 热心指数 收起 理由
admin_kefu + 30 + 20 + 2 热心帮助其他会员

总评分: 经验 + 30  论坛币 + 20  热心指数 + 2   查看全部评分

报纸
guanren 发表于 2017-7-31 21:08:26
Libaoxinxin 发表于 2017-7-31 19:43
The most common cause of this error message is that you are trying to use a string variable with a c ...
用了你说的方法,但是得到了蓝色字体的代码

地板
guanren 发表于 2017-7-31 21:08:28
Libaoxinxin 发表于 2017-7-31 19:43
The most common cause of this error message is that you are trying to use a string variable with a c ...
用了你说的方法,但是得到了蓝色字体的代码

7
andXand 发表于 2017-8-1 16:30:50
看看你的问题解决了没有呢?

8
yangxiaohui123 发表于 2017-8-3 11:16:56
1、encode varname, gen (newvarname),重新生成一个新的变量,内容与字符型的相同。

2、 Convert string variables to numeric variables:
        destring [varlist] , {generate(newvarlist)|replace} [destring_options]字符转数值
    Convert numeric variables to string variables:
        tostring varlist , {generate(newvarlist)|replace} [tostring_options]数值转字符

9
ymdtb 学生认证  发表于 2017-8-4 10:00:01
1、encode varname, gen (newvarname),重新生成一个新的变量,内容与字符型的相同。

2、 Convert string variables to numeric variables:
        destring [varlist] , {generate(newvarlist)|replace} [destring_options]字符转数值
    Convert numeric variables to string variables:
        tostring varlist , {generate(newvarlist)|replace} [tostring_options]数值转字符

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
jg-xs1
拉您进交流群
GMT+8, 2025-12-26 02:35