(Add acute accents below as appropriate.)
Herve solved his problem with various assists, notably from Svend Juul.
Still up in the air is his report about -tostring-.
-tostring- is in essence designed to be a smart wrapper for -string()-.
It is not, however, smart about dates.
Recall that to Stata a daily date is just an integer like 15156. So, by default, -tostring- will just map that to the equivalent string "15156". That works; it just isn't what Herve wants.
The help shows various ways round this. For example, you can say
tostring fyr, generate(fyr2) usedisplayformat
but that won't get you a result. The main purpose of -tostring- is to protect you from nasty side-effects of using -string()-. Otherwise, you might as well just use -string()- directly. The main way -tostring- does that is to check that your transformation is reversible, i.e. that -real(string())- will give you back what you started with. If your transformation is reversible, -tostring- loses no information. If it isn't, -tostring- won't proceed unless you insist.
So for example -real(string(15156))- certainly gives you back 15156, which is fine. But with Herve's display format you would be trying e.g. -real("30 Jun 01")- and that will give you missing because -real()- doesn't know about dates. In fact, the criterion that -real(string()) returns the original is too strict for dates, but -tostring- doesn't know about dates.
The ways forward are, it seems,
1. Using -string()- directly, as Svend recommended.
2. Adding the -force- option in -tostring-. You are saying that you know more than -tostring-. Fine if true, dangerous if not. It's only a program.
3. StataCorp to think about doing something to -tostring-. My own take is that this is not classifiable as a bug, just something that bites occasionally and for which there are easy work-arounds.
Nick
[email protected]
Herve STOLOWY
I tried to convert the date into string with:
tostring fyr, generate(fyr2)
but it does not work. I get a variable with numbers:
+-------------------+
| fyr fyr2 |
|-------------------|
1. | 30 Jun 01 15156 |
2. | 30 Jun 02 15521 |
3. | 30 Jun 03 15886 |
4. | 31 Dec 04 16436 |
5. | 31 Dec 05 16801 |
Would you know how to make this conversion?
*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/