Hi Stata users
I have some data organized in 14 folders, each folder contains a number of .csv files that have similar names that only varies by a number.
I have written the below code to import the .csv files. Because the varying number in the filenames are different for each folder (but always between 100 and 130) I use the "capture confirm file" command.
The code almost works as intendend, but has the following problem - Whenever Stata finds a .csv filename in a folder - it runs the code in the if_rc==0 bracket as intendend, but then it jumps to next step in the "foreach" loop instead of the next step in the "forvalues" loop. The result being that it only imports the the .csv file with the lowest session number from each folder.
Can you see where my code fails?
I have some data organized in 14 folders, each folder contains a number of .csv files that have similar names that only varies by a number.
I have written the below code to import the .csv files. Because the varying number in the filenames are different for each folder (but always between 100 and 130) I use the "capture confirm file" command.
The code almost works as intendend, but has the following problem - Whenever Stata finds a .csv filename in a folder - it runs the code in the if_rc==0 bracket as intendend, but then it jumps to next step in the "foreach" loop instead of the next step in the "forvalues" loop. The result being that it only imports the the .csv file with the lowest session number from each folder.
Can you see where my code fails?
HTML Code:
clear all set more off foreach ID in 01 02 03 04 05 06 07 08 09 10 11 12 13 14 { cd "Q:\PhD\Validation\Raw Angles\KGC `ID'" forvalues Session = 100(1)130 { capture confirm file "Session`Session'.csv" if _rc==0 { import delimited "Session`Session'.csv", delimiter(tab) varnames(nonames) rowrange(6) capture log close log using "Q:\PhD\Validation\Cleansed Angles\Logs\KGC_`ID'_Session_`Session'log.txt", text replace * Here I do alot of dropping, renaming and generation of variables - this part works fine I think cd "Q:\PhD\Validation\Cleansed Angles" save "KGC_`ID'_Session_`Session'.dta", replace clear log close } } }
Comment