【问题标题】:Importing a date-time variable from Excel without losing 24 hour format从 Excel 导入日期时间变量而不丢失 24 小时格式
【发布时间】:2018-07-26 11:44:08
【问题描述】:

这是我在 Excel 中的数据的 sn-p:

trail_type  correct response    created_date
T_1_P_2     S       S           02-07-2018 17:42
T_1_P_1     L       L           02-07-2018 17:42
T_1_P_3     L       L           02-07-2018 17:42
T_1_P_4     L       S           02-07-2018 17:42
T_1_P_5     S       S           02-07-2018 17:42

您会注意到,日期时间采用 24 小时格式。

但是,当我在 Stata 中导入相同的数据时,我得到以下信息:

trail_type  correct response    created_date
T_1_P_2     S       S           7/2/2018 5:42
T_1_P_1     L       L           7/2/2018 5:42
T_1_P_3     L       L           7/2/2018 5:42
T_1_P_4     L       S           7/2/2018 5:42

即使在将所有内容作为字符串导入之后也会发生这种情况。

下面是我之前导入的代码:

import excel "abc.xls", sheet("xyz") firstrow allstring

有人可以指导我如何正确导入吗?

我需要保留 24 小时格式或获取 AM/PM 指示符。

奇怪的是,如果我只是简单地将 Excel 中的日期时间复制粘贴到 Stata 的数据编辑器中,24 小时格式就会被保留。

什么样的导入命令会起作用?

问题是我有数百个这样的文件,所以我需要一些代码,而不是我手动更改每个 Excel 文件中的列。

【问题讨论】:

    标签: excel date time import stata


    【解决方案1】:

    您只需将导入的日期时间变量的format 更改为所需的值。

    以下应该有效:

    . import excel "abc.xls", sheet("xyz") firstrow
    
    . format created_date %tcDD-NN-CCYY_HH:MM
    
    . list, noobs abbreviate(20)
    
      +------------------------------------------------------+
      | trail_type     correct   response       created_date |
      |------------------------------------------------------|
      |   T_1_P_2            S          S   02-07-2018 17:42 |
      +------------------------------------------------------+
    

    请注意,变量 created_date 将是数字。

    在 Stata 的命令提示符下,输入 help format 了解更多详细信息。

    【讨论】:

    • 谢谢!
    猜你喜欢
    • 1970-01-01
    • 2020-05-12
    • 2016-08-17
    • 2016-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多