Hi felllow stata users,
I am trying to write a code to import around 100 text files with unfomatted data into column. Can someone please help me with this code. I have tried ChatGPT and a few other sources but it hasn't worked. Ideally, I would like to get all files into the column at once. Also, could you please indicate how long it will take approximately to complete once I have entered the code.
Here's the code I am trying
local folder "C:\Users\Documents\Stata" // Specify the folder where the files are located
local filelist : dir "`folder'" files "*.txt" // List all files in the folder with the extension .txt
gen myvar = "" // Create an empty variable to store the file contents
foreach file of local filelist {
file open myfile using "`folder'\`file'", read // Open each file for reading
file read myfile mydata // Read a line from the file
file close myfile // Close the file
replace myvar = "`myvar' `mydata'" // Append the file contents to the myvar variable
}
Look forward to hearing from the community.
I am trying to write a code to import around 100 text files with unfomatted data into column. Can someone please help me with this code. I have tried ChatGPT and a few other sources but it hasn't worked. Ideally, I would like to get all files into the column at once. Also, could you please indicate how long it will take approximately to complete once I have entered the code.
Here's the code I am trying
local folder "C:\Users\Documents\Stata" // Specify the folder where the files are located
local filelist : dir "`folder'" files "*.txt" // List all files in the folder with the extension .txt
gen myvar = "" // Create an empty variable to store the file contents
foreach file of local filelist {
file open myfile using "`folder'\`file'", read // Open each file for reading
file read myfile mydata // Read a line from the file
file close myfile // Close the file
replace myvar = "`myvar' `mydata'" // Append the file contents to the myvar variable
}
Look forward to hearing from the community.
Comment