Help using Base SAS procedures


Regular Contributor
Posts: 229


Hi ina a text obs i am having data like this
03/08/2009 16:45 date of this
03/08/2009 18:95 new time

In this i want the obs with out the Text like this and it should be minused form the top by below and the output should be in date and time only.

like this : dif

03/08/2009 16:45
03/08/2009 18:50 2.05
Respected Advisor
Posts: 3,896

Re: Reg:Text

Use scan() with delimiter BLANK to read the first and second word in the string.

Use a date informat for the first word in the string and assign it to a variable, something like:
MyDate=input(scan(Time_text,1,' '),ddmmyy10.);

Use a time informat for the second word in the string and assign it to a variable (i.e. MyTime).

"it should be minused form the top by below..."
It's not really clear to me if you need the difference to the first obs or just to the previous obs.
It looks for sure like one of this a bit mean (but good) excercises and I'm almost sure that the data you were given has different dates in it. That means that just calculating the difference between the values in the time variable goes wrong when you pass over midnight.

What you can do is: Create a datetime variable by using "word1" and "word2" in the string together with a datetime informat. Assign the datetime value to a variable.
The datetime value can also be created by summing the value in MyTime with the value in MyDate. As MyDate is a date value (=days since 1/1/1960) and a datetime value is in seconds since 1/1/1960 the MyDate value has to be multiplied by the seconds of a day (86400) before summing with MyTime (which should already be a value in seconds).

Then just use the lag() function, i.e. timedif=lag(datetimevar)-datetimevar;
The value you get this way is the difference in seconds. Apply a format (could also be a picture format) or use functions like hour() to present the value in hours and minutes.

Ask a Question
Discussion stats
  • 1 reply
  • 2 in conversation