Hi all,
I am really quite new to Stata, so thanks in advance for the help. I am trying to loop through an excel file that has 8 sheets, so I can modify it. Most stuff within the loop works fine, but the loop itself is not working. Here's the code for it below. I run this and the error says "unrecognized command: invalid command name ". (I am eventually going to try to nest this loop within another that goes through all files in a directory)
I have also tried the below code snippet, which I took from this posting: https://www.statalist.org/forums/for...heets-to-stata.
The error message is "option sheet() incorrectly specified" which is odd because I have it the same way as on the linked code.
I was looking into the SSC xls2dta capabilities as well, since "allsheets" command will let me import and save all sheets of an excel file as .dta as once.. but i am under the impression that you are unable to modify the excel file before sending it off as .dta. Perhaps someone experienced could confirm or deny this.
Thanks again for the help.
I am really quite new to Stata, so thanks in advance for the help. I am trying to loop through an excel file that has 8 sheets, so I can modify it. Most stuff within the loop works fine, but the loop itself is not working. Here's the code for it below. I run this and the error says "unrecognized command: invalid command name ". (I am eventually going to try to nest this loop within another that goes through all files in a directory)
Code:
local sheets = `" "ag1" "ag2" "ag3" "ag4" "ag5" "ag6" "ag7" "ag8” "' foreach x of local sheets{ import excel using /Users/andrea/Desktop/testapple.xlsx, sheet("`x'") firstrow //rename (C D E F G) y#, addnumber //rename y# ag("`x'")#, renumber(1995) sort //reshape long ag("`x'"), i(Cd) j(year) //gen crop = “apple” save testapple_`x’.dta }
I have also tried the below code snippet, which I took from this posting: https://www.statalist.org/forums/for...heets-to-stata.
The error message is "option sheet() incorrectly specified" which is odd because I have it the same way as on the linked code.
Code:
foreach x in `" "ag1" "ag2" "ag3" "ag4" "ag5" "ag6" "ag7" "ag8" "' { import excel using /Users/andrea/Desktop/testapple.xlsx, sheet("`x'") firstrow save file_`x’.dta }
Thanks again for the help.
Comment