【问题标题】:Create date variable from time (Using SAS 9.3)从时间创建日期变量(使用 SAS 9.3)
【发布时间】:2015-01-27 14:29:05
【问题描述】:

使用 SAS 9.3

我有两个变量(时间和脉冲)的文件,每个人一个文件。 我有他们开始为每个人测量的日期的信息。 现在我想创建一个日期变量,它在午夜更改日期(当然),如何?

文本文件示例:

23:58:02    106
23:58:07    105
23:58:12    103
23:58:17    98
23:58:22    100
23:58:27    97
23:58:32    99
23:58:37    100
23:58:42    99
23:58:47    104
23:58:52    95
23:58:57    96
23:59:02    98
23:59:07    96
23:59:12    104
23:59:17    109
23:59:22    105
23:59:27    111
23:59:32    111
23:59:37    104
23:59:42    110
23:59:47    100
23:59:52    106
23:59:57    114
00:00:02    123
00:00:07    130
00:00:12    130
00:00:17    125
00:00:22    119
00:00:27    116
00:00:32    122
00:00:37    116
00:00:42    119
00:00:47    117
00:00:52    114
00:00:57    114
00:01:02    110
00:01:07    103
00:01:12    98
00:01:17    98
00:01:22    102
00:01:27    97
00:01:32    99
00:01:37    93
00:01:42    97
00:01:47    103
00:01:52    96
00:01:57    93
00:02:02    93
00:02:07    95
00:02:12    106
00:02:17    99
00:02:22    102
00:02:27    96
00:02:32    93
00:02:37    97
00:02:42    102
00:02:47    101
00:02:52    95
00:02:57    92
00:03:02    100
00:03:07    95
00:03:12    102
00:03:17    102
00:03:22    109
00:03:27    109
00:03:32    107
00:03:37    111
00:03:42    112
00:03:47    113
00:03:52    115 

【问题讨论】:

  • 您使用哪种语言?你试过什么?你能分享一些示例代码吗?
  • 糟糕,抱歉,忘记告诉您有关语言的信息。 SAS。我尝试过使用保留和使用计数器。当延迟时间变量从 23 变为 0 时,还尝试增加日期变量。
  • 你实际上在做什么?发布您现在拥有的代码,以便我们了解它的适用性。

标签: sas


【解决方案1】:

正则表达式:

\d{2}:\d{2}:\d{2} \d*

查看示例并使用正则表达式: https://regex101.com/r/xF1fQ5/1

编辑:并查看 SAS 正则表达式提示表:http://support.sas.com/rnd/base/datastep/perl_regexp/regexp-tip-sheet.pdf

【讨论】:

  • 如果我有几天的时间,那如何解决问题?
【解决方案2】:

类似这样的:

Date lastDate = startDate;

List<NData> ListData = new ArrayList<NData>();
for(FileData fdat:ListFileData){
   Date nDate = this.getDate(lastDate,fdat.gettime());
   NData ndata= new NData(ndate,fdat.getMeasuring());
   LisData.add(nData);
   lastDate = nDate; 
 } 


. 
.
.
.
function Date getDate(Date ld,String time){

   Calendar cal = Calendar.getInstance();
   cal.setTime(ld);
   int year = cal.get(Calendar.YEAR);
   int month = cal.get(Calendar.MONTH)+1;
   int day = cal.get(Calendar.DAY_OF_MONTH);
   int hourOfDay   = this.getHour(time); 
   int minuteOfHour = this.getMinute(time);

   org.joda.time.LocalDateTime lastDate = new org.joda.time.LocalDateTime(ld)    
   org.joda.time.LocalDateTime newDate =  new org.joda.time.LocalDateTime(year,month,day,hourOfDay,minuteOfHour);

   if(newDate.isBefore(lastDate)){
     newDate = newDate.plusDays(1); 
   }
   return newDate.toDate();    

}

【讨论】:

  • 那是 JAVA 吗?我正在尝试在 SAS 中找到我的解决方案
【解决方案3】:

如果没有示例代码,很难提供完整的答案,但 SAS lag() 函数可能足以满足您的需求。假设您的时间变量称为time 并且您的日期变量称为date,您的数据步骤将包括如下行:

retain date;
if time < lag(time) then date = date + 1;

这假设您永远不会有任何 24 小时间隔(但看来您无论如何都必须假设)。

此答案还假定 time 字段已采用 SAS 时间格式。

【讨论】:

  • 这正是我尝试过的。但结果是只有午夜之后的第一行才能获得新的日期。之后的行再次获取之前的日期:(似乎保留功能无法按我的意愿工作。
  • 也许您有一行将初始值分配给“日期”?如果是这样,您应该在它前面加上一个条件,以便它只在第一行起作用,如下所示: if _n_ = 1 then date = '01jan2014'd;例如。
猜你喜欢
  • 2013-06-15
  • 1970-01-01
  • 2014-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-19
  • 1970-01-01
相关资源
最近更新 更多