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?