Dates, Formating, and Cutting out Characters

#1
I have two variables named "starttime" and "endtime". Both are stored as doubles. And, both are formatted "YYYY-MM-DD HH:MM:SS".

1) How might I be able to changes these dates into "YYYY-MM" format?
2) How can I change each date to a different month? (Example, the data reads "2006-11", but I want to turn it into "2006-10")

Thanks for taking the time to read this! I appreciate your help!
 
#2
1) This is easily done in Excel with the "left()" function.
2) Can you be more precise. Du you want all months to be the same or add 1 month etc?
 
#3
For
1) My dataset is too large to load in stata, so I can't use the left() function
2) I want all the dates to be minus 1 month. Ex: I want to change Dec-2010 to Nov-2010.

Thanks!
 

bukharin

RoboStataRaptor
#5
Actually it's much easier than this; Stata handles dates very well.
Code:
* convert time from clock to day format, then from day to month format
gen startmonth=mofd(dofc(starttime))
format %tm startmonth

* subtract 1 month
gen minus1month=startmonth - 1
format %tm minus1month
You could of course combine these commands:
Code:
gen minus1month=mofd(dofc(starttime)) - 1
format %tm startmonth
Since the month format counts months (as its name implies), you can work out the difference in months from starttime to endtime as:
Code:
gen monthselapsed=mofd(dofc(endtime)) - mofd(dofc(starttime))
You said that your dates were stored as doubles, but then mentioned converting from a string variable. If they're really stored as doubles ("clock" time) then you don't need to convert to string. If they're string then you need to convert back to a numeric value, eg:
Code:
gen startmonth=mofd(date(starttime, "YMD"))
See -help dates_and_times- for more info. It's a bit complex but it's well worth reading.