从时间创建日期变量(使用SAS 9.3)
使用sas9.3 我有两个变量(时间和脉冲)的文件,每个人一个文件。 我有他们开始测量每个人的日期的信息。 现在我想创建一个日期变量,它在午夜更改日期(当然),如何更改 来自文本文件的示例:从时间创建日期变量(使用SAS 9.3),sas,Sas,使用sas9.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 10
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正则表达式提示表:类似以下内容:
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();
}
Date lastDate=startDate;
List ListData=new ArrayList();
for(FileData fdat:ListFileData){
Date nDate=this.getDate(lastDate,fdat.gettime());
NData NData=新的数据(ndate,fdat.getMeasuring());
LisData.add(nData);
lastDate=nDate;
}
.
.
.
.
函数日期getDate(日期ld,字符串时间){
Calendar cal=Calendar.getInstance();
校准设定时间(ld);
int year=cal.get(日历年);
int MOUNT=cal.get(日历月)+1;
int day=cal.get(日历日/月日);
int hourOfDay=这个.getHour(时间);
int minuteOfHour=这个.getMinute(时间);
org.joda.time.LocalDateTime lastDate=新org.joda.time.LocalDateTime(ld)
org.joda.time.LocalDateTime newDate=new org.joda.time.LocalDateTime(年、月、日、小时、分钟);
if(newDate.isBefore(lastDate)){
newDate=newDate.plusDays(1);
}
返回newDate.toDate();
}
如果没有示例代码,很难提供完整的答案,但是SASlag()
函数可能足以满足您的需要。假设时间变量名为time
,日期变量名为date
,则数据步骤将包括如下行:
retain date;
if time < lag(time) then date = date + 1;
保留日期;
如果时间<滞后(时间),则日期=日期+1;
这假设你从来没有任何24小时的休息时间(但看起来你无论如何都必须假设)
此答案还假设
时间
字段已采用SAS时间格式。您使用哪种语言进行此操作?你试过什么?你能分享一些示例代码吗?Wops,对不起,忘了告诉你语言。SAS。我试过使用retain和counter。当滞后时间变量从23变为0时,也尝试增加日期变量。你们实际上在做什么?发布您现在拥有的代码,这样我们就可以看到它是如何适合的。如果我有几天的时间,那如何解决问题?是JAVA吗?我正试图找到我的解决方案,这正是我所尝试的。但结果是,只有午夜后的第一行才能得到新的日期。之后的行再次获得日期:(似乎retain函数没有按我所希望的那样工作。也许您有一行将初始值分配给“date”?如果是这样,您应该在它前面加上一个条件,使其仅在第一行工作,例如:如果_n_uu=1,那么date='2014年1月1日;例如。
retain date;
if time < lag(time) then date = date + 1;