Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Problem using date() and format


From   Nick Cox <njcoxstata@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Problem using date() and format
Date   Wed, 5 Sep 2012 17:10:44 +0100

The key point missed by Jen is that -date()- produces daily dates. In
a sense, the name -date()- is a hangover from early days [sic] when
daily dates were the only kind of date to which Stata paid special
attention.

Formatting them as if  they were monthly dates won't change them to
daily dates. Remember: changing the format just changes how something
is displayed; it does not change what something is.

Another way to do it is with -ym()-:

. di ym(real(substr("201209", 1, 4)), real(substr("201209", 5, 2)))
632

. di %tm ym(real(substr("201209", 1, 4)), real(substr("201209", 5, 2)))
 2012m9

On Wed, Sep 5, 2012 at 4:41 PM, Maarten Buis <maartenlbuis@gmail.com> wrote:
> On Wed, Sep 5, 2012 at 5:23 PM, Jen Zhen wrote:
>> I have a string variable with values 199301, 199302, 199303, ...,
>> 199312, 199401, and so on.
>> I would like to convert it to a Stata date variable and then format it
>> so that when I browse the dataset values will be displayed as 1993m1,
>> 1993m2, etc.
>>
>> I tried (amongst others):
>>
>> gen date=date(string,YM)
>> format date %tm
>>
>> and at least one of those 2 lines of code must have been wrong, as I
>> get shown values like 2972m1
>
> *----------- begin example ----------
> clear all
> input str6 strdate
> "199301"
> "199302"
> end
>
> gen date=date(strdate,"YM")
> replace date = mofd(date)
> format date %tm
> *------------- end example ----------
> (For more on examples I sent to the Statalist see:
> http://www.maartenbuis.nl/example_faq )
>
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index