0
votes

Using SAS 9.3

I have files with two variables (Time and pulse), one file for each person. I have the information which date they started measuring for each person. Now I want create a date variable whom change date at midnight (of course), how?

Example from text files:

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 
3
Which language are you using for this? And what have you tried? Can you share some sample code? - Rafael Barros
Wops, sorry, forgot to tell you about the language. SAS. I have tried using retain and using counter. Also tried to increase date variable when lag in time variable goes from 23 to 0. - Tobias
What are you actually doing? Post what code you have now, so we can see how it fits in. - Joe

3 Answers

0
votes

Regex:

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

See here for an example and play around with regex: https://regex101.com/r/xF1fQ5/1

EDIT: and have a look at the SAS regex tip sheet: http://support.sas.com/rnd/base/datastep/perl_regexp/regexp-tip-sheet.pdf

0
votes

Something like this:

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();    

}
0
votes

It's hard to provide a complete answer without sample code, but the SAS lag() function might be enough to do what you need. Your data step would include lines like the following, assuming your time variable is called time and your date variable is called date:

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

This assumes you never have any 24 hour gaps (but it appears you'd have to assume that anyway).

This answer also assumes that the time field is already in a SAS time format.