0

I have a variable ShiftStart that is a numeric variable in the format 01jan2014 06:59:59 (and so on). I want to change this to a string variable so that I can then substring it and create variables based on just date and just time separately.

When I try

generate str20 string_shiftstart=string(ShiftStart)

I create a string but all of the cells have been converted to strange values ("1.70e+12" and so on).

How can I keep the original contents of ShiftStart when it is converted to a string?

3
  • The actual storage type and display format you can get running describe. Commented Feb 18, 2015 at 5:15
  • 1
    One-line summary for those learning dates. Date-times are numeric variables. In general, all you should want to do with them is (a) convert them to other numeric variables (b) change the display format; conversion to strings is rarely necessary or useful. Commented Feb 18, 2015 at 9:50
  • 1
    This isn't really a programming problem in my view and would fail under one criterion for downvoting of "does not show any research effort". Please read the fine manual and consider posting beginner questions like this on Statalist (although on any forum there is an expectation that you have studied the documentation). Commented Feb 18, 2015 at 9:53

1 Answer 1

2

It seems you have a variable formatted as datetime. If so, no need to convert to string. There are appropriate functions that allow you to manipulate the original variable. This is clearly explained in help datetime:

clear
set more off

*----- example data -----

set obs 5

gen double datet = _n * 100000000
format datet %tc

list

*----- what you want -----

gen double date = dofc(datet)
format %td date

gen double hour =  hh(datet) + mm(datet)/60 + ss(datet)/3600

list

The reason you find your original result surprising is because you are not aware of the fact that underlying the datetime display format, is a numerical value.

A good read (aside from help datetime) is

Stata tip 113: Changing a variable's format: What it does and does not mean, The Stata Journal, by Nicholas J. Cox.

Edit

To answer your last question:

If you want to create an indicator variable marking pre/post periods, one way is using td() (see the help file). Following the example given above:

// before 04jan1960
gen pre = date < td(04jan1960)

Creating this indicator variable is not always necessary. Most commands allow the use of the if qualifier, and you can insert the condition directly. See help if.

If you mean something else, you should be more explicit.

Sign up to request clarification or add additional context in comments.

4 Comments

That was useful, thank you. Once I have changed the date to %td, how can I divide the dates into two periods (pre and post)? The newly formatted ShiftStart variable is now in the form of 1.70e+12 (and so forth).
I've made in edit to my original answer. Please see also stackoverflow.com/help/someone-answers, on what to do when someone answers your question. This will increase your reputation as well and give you certain privileges at the Stack Overflow site.
The wording of your first comment implies that you have not quite grasped the point. Changing the format affects only what is displayed. You need to calculate one kind of value (e.g. daily dates in days since 1 January 1960) from datetimes (milliseconds since the same origin). That's much more than a format change.
Roberto Ferrer is gently hinting that as you found his answer helpful, you should accept it, so that he gets the points (and you do too).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.