Need help with time

#1
Hello. I was hoping someone could help me work with time in stata.

Essentially, my problem is that every time I reformat a time variable into %tc format and save my table, it returns to numeric form every time I open it up again.

For example, I reformat "548560" to say "00:09:08" and save, but every time I open the table again, it's back to "548560".

Is there any way I can save things so that the results are always displayed as HH:MM:SS and exported to a raw datafile as such? Also, it is possible to have the duration just turn up as minutes, instead of 01June1960?

background:
My original data is a string variable called "time" looking something like "14:23:58" and showing the time something started. I first converted it from string to time using,

generate time3 = clock(time, "hms")

then I calculated durations using

generate task_time= time3[_n+1] - time3

Then I export this task_time variable to a new table.
It is in this table that I want the time to read 00:09:08 every time I open it up instead of "548560". Also, is there any way to make it just be the actual minutes instead of 01Jan09 00:09:08 (I mean beyond formatting it as %tcHH:MM:SS), i.e. I don't want it to be time of day on a certain day, just actual minutes.

Thanks.
 

bukharin

RoboStataRaptor
#2
To get the display format you're after:
format %tcHH:MM:SS time3

(see -help dates_and_times-)

If you save a dataset as a Stata file after formatting a variable, it should save the formatting too. It sounds like the problem with the disappearing format is caused by your method of exporting - how are you doing this? And what do you mean by a "raw datafile"?

If all else fails you could convert time3 back to a string variable with the format you've chosen:
tostring task_time, gen(task_time_str) usedisplayformat force
This should then be exported correctly to any format