Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Hot to get a list of observations name - Google trends data

    Hi all,
    I'll try to be as clear as possible.
    I've to download google trends data regarding a list of ticker name (ticker name is the identification name for a stock on the stock exchange).
    I explore if STATA has some packages to do it, nevertheless it seems that it doesn't (if someone knows something, please let me know).

    So, I found that with python you can do it. I found the codes on python and it seems that the input that you have to insert in one line of codes is the following:
    Code:
    KEYWORDS=['AAPL','FB','AMZN',]
    Now, since I want to facilitate the process, my objective is to copy from STATA my ticker and then paste on python.
    I Have a panel data as this one
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input double fyear str8 tic
    1987 "0015B"
    1988 "0015B"
    1989 "0015B"
    1990 "0015B"
    1991 "0015B"
    1992 "0015B"
    1993 "0015B"
    1994 "0015B"
    1995 "0015B"
    1996 "0015B"
    1997 "0015B"
    1998 "0015B"
    1999 "0015B"
    2000 "0015B"
    2001 "0015B"
    2002 "0015B"
    2003 "0015B"
    2004 "0015B"
    2005 "0015B"
    2006 "0015B"
    2007 "0015B"
    2008 "0015B"
    2009 "0015B"
    2010 "0015B"
    2011 "0015B"
    2012 "0015B"
    2013 "0015B"
    2000 "0030B"
    2001 "0030B"
    2002 "0030B"
    2003 "0030B"
    2004 "0030B"
    2005 "0030B"
    2006 "0030B"
    2007 "0030B"
    2008 "0030B"
    2009 "0030B"
    2010 "0030B"
    2011 "0030B"
    2012 "0030B"
    2013 "0030B"
    1987 "0032A"
    1988 "0032A"
    1989 "0032A"
    1990 "0032A"
    1991 "0032A"
    1992 "0032A"
    1993 "0032A"
    1994 "0032A"
    1995 "0032A"
    1996 "0032A"
    1997 "0032A"
    1998 "0032A"
    1999 "0032A"
    2000 "0032A"
    2001 "0032A"
    2002 "0032A"
    2003 "0032A"
    2004 "0032A"
    2005 "0032A"
    2006 "0032A"
    2007 "0032A"
    2008 "0032A"
    2009 "0032A"
    2010 "0032A"
    2011 "0032A"
    2012 "0032A"
    2013 "0032A"
    2014 "0032A"
    2015 "0032A"
    1987 "0033A"
    1988 "0033A"
    1989 "0033A"
    1990 "0033A"
    1991 "0033A"
    1992 "0033A"
    1993 "0033A"
    1994 "0033A"
    1995 "0033A"
    1996 "0033A"
    1997 "0033A"
    1998 "0033A"
    1999 "0033A"
    2000 "0033A"
    2001 "0033A"
    2002 "0033A"
    2003 "0033A"
    2004 "0033A"
    2005 "0033A"
    2006 "0033A"
    2007 "0033A"
    2008 "0033A"
    2009 "0033A"
    2010 "0033A"
    2011 "0033A"
    2012 "0033A"
    2013 "0033A"
    2014 "0033A"
    2015 "0033A"
    2003 "0038A"
    end
    As you can see the ticker it repeats over the time.
    I can overcome this problem with
    Code:
    duplicates drop tic fyear, force
    Nevertheless, how can extract on a txt file the tic with the condition that Python needs, in other word with the 'tickername', (appendix and comma after the ticker name).

    Thank you in advance for your kind support

  • #2
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input double fyear str8 tic
    1987 "0015B"
    1988 "0015B"
    1989 "0015B"
    1990 "AAPL"
    1991 "AAPL"
    1992 "FB"
    1993 "FB"
    1994 "AMZN"
    1995 "AMZN"
    1996 "AMZN"
    1997 "AMZN"
    end
    
    contract tic
    levelsof tic, local(tics) sep(',') clean
    local wanted= "KEYWORDS=['`tics'',]"
    di "`wanted'"
    Res.:

    Code:
    . 
    . di "`wanted'"
    KEYWORDS=['0015B','AAPL','AMZN','FB',]

    Comment


    • #3
      Many thanks, Andrew Musau !

      Just one thing. If I run this codes, in the output windows it seems that STATA doesn't display the firsts ticker (as you can see in the screenshoot below, it starts from those with letter "C", missing all the initially ones)
      I'm just wondering if I can have the whole output in txt file, also avoiding the symbol ">" that STATA puts in every row.
      Many thanks in advance for your time and kind suggestion





      Click image for larger version

Name:	ScreenshootSTATA.png
Views:	1
Size:	109.2 KB
ID:	1570960

      Comment


      • #4
        Try this

        Code:
        clear
        input double fyear str8 tic
        1987 "0015B"
        1988 "0015B"
        1989 "0015B"
        1990 "AAPL"
        1991 "AAPL"
        1992 "FB"
        1993 "FB"
        1994 "AMZN"
        1995 "AMZN"
        1996 "AMZN"
        1997 "AMZN"
        end
        
        contract tic
        levelsof tic, local(tics) sep(',') clean
        local wanted= "KEYWORDS=['`tics'',]"
        di "`wanted'"
        
        file open ticktxt using ticks.txt, write text replace
        file write ticktxt ("`wanted'") _newline
        file close ticktxt
        type ticks.txt
        Code:
        (some output omitted)
        
        . type ticks.txt
        KEYWORDS=['0015B','AAPL','AMZN','FB',]

        Comment


        • #5
          Thank you for you answer Leonardo Guizzetti . However when I open ticks.txt the file is empty
          Do you have any suggestions for the solution?
          Thank you, it seems we are close to the solve it
          Last edited by Marco Errico; 01 Sep 2020, 11:02.

          Comment


          • #6
            This code was tested to run from a do-file, not entering commands one by one into the Stata window (though that should still work in this particular case). This works on my machine (just tested again) so I suspect that you might be mixing between a do-file and interactive mode.

            Comment


            • #7
              Thank you Leonardo. I don't know why when I run it the second time it worked properly.
              Thanks because it will save me a lot of time.
              A little OT: to your knowledge there is no way to download Google trends data with stata packages? Otherwise I'll dig in Python

              Comment


              • #8
                Originally posted by Marco Errico View Post
                Thank you Leonardo. I don't know why when I run it the second time it worked properly.
                Thanks because it will save me a lot of time.
                A little OT: to your knowledge there is no way to download Google trends data with stata packages? Otherwise I'll dig in Python
                I don't work with Google Trends so I wouldn't know if there are any packages that do this.

                Comment

                Working...
                X