Announcement

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

  • Naming tempfiles

    I wanna use the stockquote command to get some stock market data. The synatax
    Code:
    stockquote ^GSPC, start_date(2021-01-01) end_date(2022-06-01)
    works fine, the issue is when we start to loop them together. Observe:
    Code:
    loc stocks ^GSPC LSEG.L ^DJI ^IXIC ^N100 ^NYA ^VIX GD LMT NOC RTX
    clear
    cls
    set tr off
    
    foreach x of loc stocks {
        
    tempfile "`x'"
    
    
    stockquote `x', start_date(2021-01-01) end_date(2022-06-01)
    
    g stock = "`x'"
    
    keep date stock adjclose
    
    order stock date, first
    
    rename adj price
    
    mer m:1 stock using `stockdata', nogen
    
    qui sa ``x'', replace
    
    }
    
    foreach x of loc stocks {
        
        
    ap using ``x''
        
    }
    When I try, though, Stata tells me
    Code:
    _"^GSPC invalid name
    r(198);
    the first time I try and declare the tempfile. Yet when I do
    Code:
    sa ^GSPC, replace emptyok
    Stata sees no issue with this. All I'd like is to make a tempfile with the names of these stock indices which must have the ^ sign preceding them- the code otherwise runs fine. How might I loop all these together?

  • #2
    The tempfile command expects to see a local macro name. Local macro names, as all names in Stata, cannot start with or contain ^ and/or quotes. You use the local macro name to refer to the temporary file. The actual filename that Stata uses internally is something like ST_160c_000001.tmp and there is no way to change that (and still have Stata automatically delete the file).


    Edit: I was not sure whether you want suggestions on how to proceed. There are many options. Here is one:

    Code:
    code omitted
    
    local count_files 0                     // <- new line
    
    foreach x of loc stocks {
    
    tempfile `++count_files'                // <- changed line
    
    stockquote `x', start_date(2021-01-01) end_date(2022-06-01)
    
    more code referring to `x' (the original stock index name)
    
    qui sa "``count_files''", replace
    
    }
    
    local --count_files                     // <- new line; probably wanted
    
    forvalues i = 1/`count_files' {         // <- changed line
        append using "``i''"
    }
    Note that your original code has the last file in memory after the first loop concludes and then appends that same file at the end of the second loop; that is probably not what you want.
    Last edited by daniel klein; 15 Jul 2022, 00:11.

    Comment


    • #3
      Daniel explained what the problem is. Apart from Daniel's suggestions you might check out the somewhat underappreciated function -strtoname()-. Some of these constructs might turn out useful:

      Code:
      . tempfile _^temp
      _^ invalid name
      r(198);
      
      . dis strtoname("^temp")
      _temp
      
      . tempfile `=strtoname("^temp")'

      Comment


      • #4
        Thank you both. I'll check this out soon as I get outta bed and have coffee.

        Comment

        Working...
        X