Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using local within local variable name

    How can I do the following using a loop? I'm weary as there is a local file within the quotation signs and so, I cannot use loop anymore.

    Code:
    tempfile file1
    import excel using "file1.xlsx", firstrow clear
    save `file1', replace
    
    tempfile file2
    import excel using "file2.xlsx", firstrow clear
    save `file2', replace
    
    tempfile file3
    import excel using "file3.xlsx", firstrow clear
    save `file3', replace
    
    use `file1', clear
    append `file2'
    append `file3'
    I cannot do the following, especially `file`i'' line.
    Code:
    forval i=1/3 {
        tempfile file`i'
        import excel using "file`i'.xlsx", firstrow clear
        save `file`i'', replace
    }
    
    use `file1', clear
    append `file2'
    append `file3'
    Any way I can get around this? Thank you!

  • #2
    Does your code without the loops work successfully? There is nothing wrong with the syntax of your commands. The one change I would make is replace
    Code:
        save `file`i'', replace
    with
    Code:
        save "`file`i''", replace
    in case there are spaces embedded in the path to the temporary file. But I would have expected that to also be a problem in the code without the loops.

    Comment

    Working...
    X