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 at the end of May, 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   Phil Schumm <pschumm@uchicago.edu>
To   <statalist@hsphsun2.harvard.edu>
Subject   Re: st: Translate Google Trends date system into Stata date system
Date   Wed, 1 May 2013 12:09:06 -0500

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/


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