Announcement

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

  • Using integ function in a loop

    Hi,

    I have a set of datafiles - file_1 to file_10. Each file looks like the following:
    Y X
    9 1
    15 2
    6 3
    23 4
    I wanted to run a loop that gets by each file, using the integ function to numerically integrate y on x and generate a new column with the value of the integrals, called a.

    I wrote a loop like this:

    forvalues i = 1/10{
    use file_`i', clear
    integ y x, generate(a)
    save file_`i', replace
    }

    Curiously enough, I can't run this even if I can run the inner commands by themselves. The error I get is "no observations", r(2000). Can you please tell me where I am going wrong?

    Thanks!
    Haaris

  • #2
    The code below, which is exactly what I understand you are trying, runs fine.

    Troubleshooting this will require an example of your actual data with dataex, see the FAQ on how and why: https://www.statalist.org/forums/help#stata
    One issue could be one of your variables is string. Other issues might also be spotted if we see an example of your data as is.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte(y x)
     9 1
    15 2
     6 3
    23 4
    end
    
    
    save file_1, replace
    save file_2, replace
    save file_3, replace
    
    
    forvalues i = 1/3{
    use file_`i', clear
    integ y x, generate(a)
    save file_`i', replace
    }

    Comment

    Working...
    X