楼主: heyishine
5106 5

[数据管理求助] 请问如何将yyyymmdd格式的日期数据修改为数值型或者日期型数据? [推广有奖]

  • 0关注
  • 0粉丝

高中生

17%

还不是VIP/贵宾

-

威望
0
论坛币
0 个
通用积分
0
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
142 点
帖子
10
精华
0
在线时间
33 小时
注册时间
2013-7-13
最后登录
2018-4-3

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

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

经管之家联合CDA

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

感谢您参与论坛问题回答

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

+2 论坛币
万分感谢!
二维码

扫码加我 拉你入群

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

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

关键词:数据修改 日期数据 日期型 数值型 万分感谢 如何

沙发
lyrasover 发表于 2013-7-13 12:24:58 |只看作者 |坛友微信交流群
用encode命令即可将原格式日期数据转成数值型:encode a, generate(b)。斜体为变量名

使用道具

藤椅
蓝色 发表于 2013-7-13 13:32:32 |只看作者 |坛友微信交流群
Title

    [D] datetime translation -- String to numeric date translation functions


Syntax

    The string-to-numeric date and time translation functions are

            Desired SIF type  |  String-to-numeric translation function
            ------------------+---------------------------------------
            datetime/c        |        clock(HRFstr, mask [, topyear])
            datetime/C        |        Clock(HRFstr, mask [, topyear])
                              |
            date              |         date(HRFstr, mask [, topyear])
                              |
            weekly date       |       weekly(HRFstr, mask [, topyear])
            monthly date      |      monthly(HRFstr, mask [, topyear])
            quarterly date    |    quarterly(HRFstr, mask [, topyear])
            half-yearly date  |   halfyearly(HRFstr, mask [, topyear])
            yearly date       |       yearly(HRFstr, mask [, topyear])
            ----------------------------------------------------------

    where

        HRFstr is the string value (HRF) to be translated,

        topyear is described in Working with two-digit years, below,

        and mask specifies the order of the date and time components and is a string composed of a sequence of these elements:

            Code  | Meaning
            ------+---------------------------------------
             M    | month
             D    | day within month
             Y    | 4-digit year
             19Y  | 2-digit year to be interpreted as 19xx
             20Y  | 2-digit year to be interpreted as 20xx
                  |
             h    | hour of day
             m    | minutes within hour
             s    | seconds within minute
                  |
             #    | ignore one element
            ----------------------------------------------

        Blanks are also allowed in mask, which can make the mask easier to read, but they otherwise have no significance.

        Examples of masks include

            "MDY"      HRFstr contains month, day, and year, in that order.

            "MD19Y"    means the same as "MDY" except that HRFstr may contain two-digit years, and when it does, they are to be treated as if they are
                       4-digit years beginning with 19.

            "MDYhms"   HRFstr contains month, day, year, hour, minute, and second, in that order.

            "MDY hms"  means the same as "MDYhms"; the blank has no meaning.

            "MDY#hms"  means that one element between the year and the hour is to be ignored.  For example, HRFstr contains values like "1-1-2010 at
                       15:23:17" or values like "1-1-2010 at 3:23:17 PM".


Description

    These functions translate dates and times recorded as strings containing human readable form (HRF) to the desired Stata internal form (SIF).  See [D]
    datetime for an introduction to Stata's date and time features.

    Also see Using dates and times from other software in [D] datetime.


Remarks

    Remarks are presented under the following headings:

            Introduction
            Specifying the mask
            How the HRF-to-SIF functions interpret the mask
            Working with two-digit years
            Working with incomplete dates and times
            Translating run-together dates, such as 20060125
            Valid times
            The clock() and Clock() functions
            Why there are two SIF datetime encodings
            Advice on using datetime/c and datetime/C
            Determining when leap seconds occurred
            The date() function
            The other translation functions


    Introduction

    The HRF-to-SIF translation functions are used to translate string HRF dates, such as "08/12/06", "12-8-2006", "12 Aug 06", "12aug2006 14:23", and "12
    aug06 2:23 pm", to SIF.  The HRF-to-SIF translation functions are typically used after importing or reading data.  You read the date information into
    string variables and then the HRF-to-SIF functions translate the string into something Stata can use, namely, an SIF numeric variable.

    You use generate to create the SIF variables.  The translation functions are used in the expressions, such as

        . generate double time_admitted = clock(time_admitted_str, "DMYhms")
        . format time_admitted %tc

        . generate date_hired = date(date_hired_str, "MDY")
        . format date_hired %td

   

使用道具

板凳
heyishine 学生认证  发表于 2013-7-16 08:52:58 |只看作者 |坛友微信交流群
lyrasover 发表于 2013-7-13 12:24
用encode命令即可将原格式日期数据转成数值型:encode a, generate(b)。斜体为变量名
嗯嗯,十分谢谢。

使用道具

报纸
heyishine 学生认证  发表于 2013-7-16 08:54:33 |只看作者 |坛友微信交流群
蓝色 发表于 2013-7-13 13:32
Title

    [D] datetime translation -- String to numeric date translation functions
多谢多谢

使用道具

地板
michaeljija 发表于 2013-7-18 00:27:41 |只看作者 |坛友微信交流群
It's good for me. Thanks~

使用道具

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

本版微信群
加好友,备注jltj
拉您入交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-5-22 23:40