Announcement

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

  • Saving variables in foor loop

    Dear all,

    I am new with stata and currently trying to figure out how to make the following loop work:


    forval i=1/2 {

    import excel using U:\Stata\Test_`i', firstrow

    nl (CE = ({b0=0.5}*(y^{b1=0.8} - x^{b1=0.8})+x^{b1=0.8})^(1/{b1=0.8}))

    gen Alpha_`i'=_b[/b1]

    }

    I get following error Code: no; data in memory would be lost

    I think the key is to somehow extract Alpha as a hard value.

    Thank you in Advance.
    Stefan

  • #2
    The error presumably arises because import excel wants you to specify clear. But save your results first.

    Comment


    • #3
      Thank you Nick. So how can I solve this? Use a different import command?

      Thanks in advance.
      Stefan

      Comment


      • #4
        No; any other import command would raise the same issue. But perhaps your problem is easier than appears. If all you care about is the estimate for b1 it is given in your results. So just specify clear and loop over your files. Your code states that there are only two of them.

        Comment


        • #5
          Yes I`m only interested in b1. And so far there is only two but just so I can see if a loop would work. I really appreciate your help.

          Even if I do the following it will tell me: no; data in memory would be lost


          clear
          forval i=1/2 {
          import excel using U:\Stata\Test_`i', firstrow
          nl (CE = ({b0=0.5}*(y^{b1=0.8} - x^{b1=0.8})+x^{b1=0.8})^(1/{b1=0.8}))
          clear
          gen Alpha_`i'=_b[/b1]

          }

          Comment


          • #6
            As I implied (I hope) in #2 clear needs to be an option on import excel.

            The effect of your generate statement is to create a variable with no observations. Try describe and list afterwards to see that. That is allowed by Stata, and it thus won't let you import more data.

            I think you would be better off appending your datasets first and then looping over distinct groups of observations.

            Comment

            Working...
            X