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: Translate Google Trends date system into Stata date system


From   Nick Cox <njcoxstata@gmail.com>
To   "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu>
Subject   Re: st: Translate Google Trends date system into Stata date system
Date   Wed, 1 May 2013 18:21:59 +0100

Indeed. But ill-formed string dates are likely to produce missing or
absurd numeric dates which should make themselves obvious sooner or
later.

In a sufficiently serious script, there would be testing before and
after. For example, the example includes 5 "-" as separators which you
could test for as a universal

assert length(subinstr(mydate, "-", "", .)) == length(mydate) - 5

That is, if you replace every "-" by empty "", the date should be
precisely 5 characters shorter.


Nick
njcoxstata@gmail.com


On 1 May 2013 18:09, Phil Schumm <pschumm@uchicago.edu> wrote:
> On May 1, 2013, at 11:21 AM, Sergiy Radyakin wrote:
>> tempvar d1 d2
>> generate `d1'=substr(googledate,1,10)
>
> <snip>
>
> On May 1, 2013, at 11:41 AM, Nick Winter wrote:
>> Or, for maximum concision (and obfuscation):
>>
>> forval i=1/2 {
>>  generate date`i' = date(substr(googledate, 12-11*`i' ,10),"YMD")
>>  format date`i' %td
>> }
>
>
> As long as we're beating this poor horse, I would note that while using -substr()- is perfectly fine if the format of your input is guaranteed (as it may well be with Google Trends), it can be dangerous in cases where it is not (i.e., silently returning an undesired value).  Using a regular expression to extract the information (as I suggested) is safer in such cases, and in fact, can often be easily modified to work even in cases where the format of the input varies slightly (assuming that the desired information is still present).  Finally, some might argue that it is more readable, since a call to -substr()- tells you nothing about what you are expecting to encounter in the input.
>
>
> -- Phil
>
>
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/faqs/resources/statalist-faq/
> *   http://www.ats.ucla.edu/stat/stata/

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


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