Announcement

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

  • A For Loop problem

    I have several .csv data files that I want to import and save using a for loop.
    The file names have a pattern, namely, scd1011, scd1112, scd1213, scd1314,...,scd1516
    I am thinking of the following code, but cannot get the result
    1. forvalues i =10(1)15{
    cd "$rawdata"
    insheet using "scd`i'`i+1'.csv", clear
    cd "$workdata"
    save "scd`i'`i+1'.dta",replace
    }

    2. forvalues i =10(1)15{
    scalar j=i+1
    cd "$rawdata"
    insheet using "scd`i'`j'.csv", clear
    cd "$workdata"
    save "scd`i'`j'.dta",replace
    }

    Is there any solution to this?




  • #2
    In your second example you have j defined as scalar but then in the -insheet- command you have j as local macro.

    I think something like this would work:

    Code:
    forvalues i =10(1)15{
        local j=`i'+1
        insheet using "scd`i'`j'.csv", clear
    ...
    Or you could set up your for loop such as:
    Code:
    . forvalues i =1011(101)1516{
      2.         dis `i'
      3. }
    1011
    1112
    1213
    1314
    1415
    1516

    Comment

    Working...
    X