Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[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 <[email protected]>
To   <[email protected]>
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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index