I know my title is confusing in the sense that the tokenize
command is specified to a string.
I have many folders that contain massive, separated, ill-named Excel files (most of them are scraped from ahe website). It's inconvenient to select them manually so I need to rely on Stata extended macro function local :dir
to read them.
My code looks as follows:
foreach file of local filelist {
import excel "`file'", clear
sxpose, clear
save "`file'.dta", replace
}
Such code will generate many new dta
files and the directory is thus full of these files. I prefer to create a single new data file for the first xlsx
file and then append
others to it inside the foreach
loop. So essentially, there's a if-else
inside the loop.
We need an index of the macro filelist
just created, so that we can write something like:
token `filelist' // filelist is created in the former code
if "`i'" == `1' {
import excel "`file'",clear
}
else {
append using `i',clear
}
I know my code is inefficient and error-prone: the syntax of expression token 'filelist'
is incorrect too (given that filelist
is not a string). However, I still want to figure out the basic structure behind my pseudo code.
How could I correct my code and make it work?
Another more efficient approach is highly welcomed.