Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: date format changed to number - why?


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: date format changed to number - why?
Date   Thu, 7 Dec 2006 21:38:52 -0000

Your major question I leave to others, although 
I think that part of the answer is that you are 
confusing format -- how a variable is presented 
-- with value -- how it is held. Accidentally
you expose a detail over which I can explain the 
error of your ways. 

The loop 

foreach var in varlist datevar1 {
   format datevar1 %dCY-N-D
}

would work, but for the wrong reason. You 
probably meant 

foreach var of varlist datevar1 {
   format datevar1 %dCY-N-D
}

which would work, but does nothing that 

format datevar1 %dCY-N-D

would not do directly. 

There are two reasons for this. 

1. A loop over one thing can be replaced 
by that one thing, without scaffolding. 

2. You set up a looping macro `var', but 
never use it. 

foreadh var in varlist datevar1 { 
	format datevar1 %dCY-N-D
} 

would go round the loop twice, once
setting `var' to "varlist" and once 
setting `var' to "datevar1". But neither 
time does Stata do anything with your 
`var'. You just get the format you 
asked for, twice. 

On other occasions, that could be a bug. 

Nick 
[email protected] 

Bednar, Paul
> 
> When I import a date variable using odbc Stata changes the format from
> yyyy-mm-dd to mmddyyyy--for example, 2002-07-01 to 01Jul2002.
> 
> I need to convert the date variable back to yyyy-mm-dd to be in the
> proper database format.  I do this in the following manner 
> (variable is
> datevar1):
> 
> foreach var in varlist datevar1 {
>   format datevar1 %dCY-N-D
> }
> 
> Listing datevar1 via the command line displays the data in the proper
> yyyy-mm-dd format.  However, using odbc I get an "incorrect 
> date value:
> '1522'" error message, as the date variable somehow has been converted
> to a number (date should be 2002-07-01).
> 

*
*   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/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index