I have multiple .dta datasets and they all contain the same variable labeled "Q". I want to loop over the datasets and rename the variable with the name of the dataset( (filename). (e.g. Q should be renamed to eps from eps.dta dataset) I came up with the following code:
Code:
loc datasets : dir . files "*.dta" foreach f of local datasets { di as result "Reading `f'" use "`f'", clear local sheetname=subinstr("`f'",".dta") rename Q ("`sheetname'") save "`f'", replace }
I also looked at the https://www.statalist.org/forums/for...rename-command post, if I run the code of that post I get the error message "variable Q not found". Could somebody help me to fix my code?
Thank you very much!
Best regards,
Jonas
0 Response to Loop over files and rename variable with filename
Post a Comment