楼主: lucy9383
19295 10

[数据管理求助] 怎样将时间序列数据处理成面板数据 [推广有奖]

  • 0关注
  • 1粉丝

已卖:30份资源

博士生

20%

还不是VIP/贵宾

-

威望
0
论坛币
94 个
通用积分
1.2245
学术水平
0 点
热心指数
2 点
信用等级
0 点
经验
658 点
帖子
212
精华
0
在线时间
172 小时
注册时间
2012-7-31
最后登录
2021-11-23

楼主
lucy9383 发表于 2015-3-2 23:13:42 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
从Wind上下载下来的数据,数据格式如下:
2011 2012 2013
A
B
C
ABC分别代表股票代码,2011 2012 2013是时间,空格内是股票收益率,现在想将数据调成如下格式:
A 2011
A 2012
A 2013
B 2011
B 2012
B 2013
这个能用stata办到吗?还是得用其他别的软件?

有点儿着急解决问题,非常感谢!
二维码

扫码加我 拉你入群

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

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

关键词:时间序列数据 序列数据 数据处理 时间序列 面板数据 股票代码 收益率 2011 软件

沙发
蓝色 发表于 2015-3-3 08:37:06
看reshape命令,模仿例子去解决吧

Title

    [D] reshape -- Convert data from wide to long form and vice versa


Syntax

    Overview

           long
        +------------+                  wide
        | i  j  stub |                 +----------------+
        |------------|                 | i  stub1 stub2 |
        | 1  1   4.1 |     reshape     |----------------|
        | 1  2   4.5 |   <--------->   | 1    4.1   4.5 |
        | 2  1   3.3 |                 | 2    3.3   3.0 |
        | 2  2   3.0 |                 +----------------+
        +------------+

        To go from long to wide:

                                            j existing variable
                                           /
                reshape wide stub, i(i) j(j)

        To go from wide to long:

                reshape long stub, i(i) j(j)
                                           \
                                            j new variable

        To go back to long after using reshape wide:

                reshape long

        To go back to wide after using reshape long:

                reshape wide


    Basic syntax

        Convert data from wide form to long form

            reshape long stubnames, i(varlist) [options]

        Convert data from long form to wide form

            reshape wide stubnames, i(varlist) [options]

        Convert data back to long form after using reshape wide

            reshape long

        Convert data back to wide form after using reshape long

            reshape wide

        List problem observations when reshape fails

            reshape error

         options                    Description
       ----------------------------------------------------------------------------------------------------------------------------------------------------
       * i(varlist)                 use varlist as the ID variables
         j(varname [values])        long->wide: varname, existing variable
                                    wide->long: varname, new variable
                                    optionally specify values to subset varname
         string                     varname is a string variable (default is numeric)
       ----------------------------------------------------------------------------------------------------------------------------------------------------
        * i(varlist) is required.

        where values is  #[-#] [...]              if varname is numeric
                                                    (default)
                         "string" ["string" ...]  if varname is string

        and where stubnames are variable names (long->wide), or stubs of variable names (wide->long), and either way, may contain @, denoting where j
        appears or is to appear in the name.

        In the example above, when we wrote "reshape wide stub", we could have written "reshape wide stub@" because j by default ends up as a suffix.
        Had we written stu@b, then the wide variables would have been named stu1b and stu2b.


    Advanced syntax

        reshape i varlist

        reshape j varname [values] [, string]

        reshape xij fvarnames [, atwl(chars)]

        reshape xi [varlist]

        reshape [query]

        reshape clear


Menu

    Data > Create or change data > Other variable-transformation commands > Convert data between wide and long


Description

    reshape converts data from wide to long form and vice versa.


Options

    i(varlist) specifies the variables whose unique values denote a logical observation. i() is required.

    j(varname [values]) specifies the variable whose unique values denote a subobservation.  values lists the unique values to be used from varname,
        which typically are not explicitly stated because reshape will determine them automatically from the data.

    string specifies that j() may contain string values.

    atwl(chars), available only with the advanced syntax and not shown in the dialog box, specifies that chars be substituted for the @ character when
        converting the data from wide to long form.


Remarks

    Before using reshape, you need to determine whether the data are in long or wide form.  You also must determine the logical observation (i) and the
    subobservation (j) by which to organize the data.  Suppose that you had the following data, which could be organized in wide or long form as follows:


                                                                       (wide form)           

                                                              i         ....... x_ij ........
                                                              id  sex   inc80   inc81   inc82
                                                              -------------------------------
                                                               1    0    5000    5500    6000
                                                               2    1    2000    2200    3300
                                                               3    0    3000    2000    1000


                                                                        (long form)      

                                                                  i     j           x_ij
                                                                  id   year   sex    inc
                                                                  -----------------------
                                                                   1     80     0   5000
                                                                   1     81     0   5500
                                                                   1     82     0   6000
                                                                   2     80     1   2000
                                                                   2     81     1   2200
                                                                   2     82     1   3300
                                                                   3     80     0   3000
                                                                   3     81     0   2000
                                                                   3     82     0   1000


    Given these data, you could use reshape to convert from one form to the other:

        . reshape long inc, i(id) j(year)  (goes from top form to bottom)
        . reshape wide inc, i(id) j(year)  (goes from bottom form to top)

    See [D] reshape for a detailed discussion and examples for both the basic and advanced syntax.


Examples

    -------------------------------------------------------------------------------------------------------------------------------------------------------
    Setup
        . webuse reshape1

    List the data
        . list

    Convert data from wide form to long form
        . reshape long inc ue, i(id) j(year)

    List the result
        . list, sepby(id)

    Convert data back from long form to wide form (quick way)
        . reshape wide

    Convert data back from long form to wide form (explicit way)
        . reshape wide inc ue, i(id) j(year)

    -------------------------------------------------------------------------------------------------------------------------------------------------------
    Setup
        . webuse reshape2, clear

    List the data
        . list

    Try to convert the data from wide form to long form
        . reshape long inc, i(id) j(year)

    List the problem observations
        . reshape error

    -------------------------------------------------------------------------------------------------------------------------------------------------------
    Setup
        . webuse reshape3, clear

    List the data
        . list

    Convert the data from wide form to long form
        . reshape long inc@r ue, i(id) j(year)

    List the result
        . list, sepby(id)

    Convert the data back from long form to wide form (quick way)
        . reshape wide

    Convert the data back from long form to wide form (explicit way)
        . reshape wide inc@r ue, i(id) j(year)

    -------------------------------------------------------------------------------------------------------------------------------------------------------
    Setup
        . webuse reshape4, clear

    List the data
        . list

    Convert the data from wide form to long form, allowing sex to take on string values
        . reshape long inc, i(id) j(sex) string

    List the result
        . list, sepby(id)

    Convert the data back from long form to wide form (quick way)
        . reshape wide

    Convert the data back from long form to wide form (explicit way)
        . reshape wide inc, i(id) j(sex) string

    -------------------------------------------------------------------------------------------------------------------------------------------------------
    Setup
        . webuse reshape5, clear

    List the data
        . list

    Convert the data from long-long form to wide-wide form (two j variables)
        . reshape wide @inc, i(hid year) j(sex) string
        . reshape wide minc finc, i(hid) j(year)

    List the result
        . list

    Convert the data back from wide-wide form to long-long form
        . reshape long minc finc, i(hid) j(year)
        . reshape long @inc, i(hid year) j(sex) string

    List the result
        . list
    -------------------------------------------------------------------------------------------------------------------------------------------------------

已有 1 人评分经验 热心指数 收起 理由
SpencerMeng + 100 + 1 热心帮助其他会员

总评分: 经验 + 100  热心指数 + 1   查看全部评分

藤椅
lucy9383 发表于 2015-3-3 10:37:42
蓝色 发表于 2015-3-3 08:37
看reshape命令,模仿例子去解决吧

Title
谢谢,如果是月度数据呢?这样转换出来就会出错额

板凳
yeai7835 发表于 2017-5-11 19:17:37
直接用excel软件按股票代码排序就好了呀

报纸
bkjg 发表于 2018-1-23 16:12:08
您好,这个问题解决了吗?

地板
黃河泉 在职认证  发表于 2018-1-23 18:42:52
bkjg 发表于 2018-1-23 16:12
您好,这个问题解决了吗?
建议请用 dataex (先 ssc install dataex 并见说明) 将原始 Stata 资料中具有”代表性”的一部分资料列出,以供有意回答者实验之用,并能提供具体操作指令。并请参考 http://www.jianshu.com/p/9870080fe769,  https://bbs.pinggu.org/thread-5048204-1-1.html, 与 https://bbs.pinggu.org/thread-5917273-1-1.html

7
migenghan 发表于 2018-3-11 11:35:31
楼主解决问题了吗,我股票代码不只下载了收益率,还有其他很多财务指标,该怎么换成面板数据

8
quga 发表于 2018-5-20 17:12:53
可以尝试,分年度下,最后再把几个excel汇总,就能直接达到楼主想要的效果

9
UN格子蓝 发表于 2019-7-3 17:07:53
yeai7835 发表于 2017-5-11 19:17
直接用excel软件按股票代码排序就好了呀
我觉得在这点上excel比STATA好用,可能是我不会数据操作吧,感谢指点

10
wangbinjinguan 发表于 2022-1-4 23:26:47
只能手工复制,没有好办法。

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

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2025-12-22 04:52