Announcement

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

  • Generating variable loop error

    Dear all,

    I am running a loop for panel data. Their aim is to obtain some kind of optimal value from a regression of var1 on var2. I am having about 80 countries in the sample and when I run the loop on the first country I get the error
    "country_name" not found
    I switched, but not desired, variable "country" with the panel identifier "ID" and got
    Code:
     invalid 'and'
    I've run the code by substituting my two variables with two others and got again the
    Code:
     invalid 'and'
    error
    Thank you

    Here is my code below. Can you please tell why and where I am wrong?




    levelsof id, local(con)
    levelsof year, local(time)

    foreach i of local con{
    foreach j of local time{
    regress var1 c.ivar2##c.var2 if id == `i' and year ==`j'
    generate optiVar3`i'`j' = -_b[var2]/(2*_b[var2 #ivar2 ])
    generate optvar4`i'`j' = _b[var2 ]*var2 + ... ...
    }
    }

    Giorgio!
    Last edited by Giorgio Di Stefano; 19 Feb 2022, 00:49.

  • #2
    Stata syntax is easy to write, but StataCorp hasn't gotten to incorporating the "and" yet!

    Code:
    regress var1 c.ivar2##c.var2 if id == `i' & year ==`j'

    Comment


    • #3
      help statsby would be an easier framework here.

      Comment


      • #4
        Originally posted by Andrew Musau View Post
        Stata syntax is easy to write, but StataCorp hasn't gotten to incorporating the "and" yet!

        Code:
        regress var1 c.ivar2##c.var2 if id == `i' & year ==`j'
        Thank you.@Andrew Musau and Nick Cox !. I had missed that. Now, after I corrected the program, I get two errors: Either
        no observations
        or
        insufficient observations
        . That's strange because I have a balanced panel.
        Any ideas please?

        Comment


        • #5
          In panel data, a specific id-year combination defines a single observation. You cannot run a regression with a single observation.

          Code:
          webuse grunfeld, clear
          xtset company year
          list if company==1 & year==1945
          xtreg invest mvalue kstock if company==3 & year==1945
          Res.:

          Code:
          . xtset company year
                 panel variable:  company (strongly balanced)
                  time variable:  year, 1935 to 1954
                          delta:  1 year
          
          . 
          . list if company==1 & year==1945
          
               +--------------------------------------------------+
               | company   year   invest   mvalue   kstock   time |
               |--------------------------------------------------|
           11. |       1   1945    561.2   4840.9      265     11 |
               +--------------------------------------------------+
          
          . 
          . xtreg invest mvalue kstock if company==3 & year==1945
          insufficient observations
          r(2001);

          Comment

          Working...
          X