Announcement

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

  • try to do a loop within a loop

    I am a beginner stata user and new to this post. I found this forum was extremely helpful when I google search for stata help.

    I have 5 dependent variables and I need to run same regressions for each of the dependent variables. For each dependent variable, I will run three sets of regressions and the only difference between the three sets of regression was one set of interaction terms were using different variables. instead of running one loop for 5 dependent variables three times for each of the three interaction terms and control variables, I think a loop for 5 dependent variables and a within loop for the three interaction terms can make it easier.

    Here are my codes:

    local ppost t_state##ib2.p_c ---i used this to create a local variable for my interaction terms, which may be wrong
    local postdum t_state##ib0.post
    local state st##ib2.p_c

    local pv `ppost' `postdum' `state'

    foreach v of varlist b2 b4 b8 b2 b16{
    foreach p of local pv{
    xi:svy: reg `v' `p' $charm if r_`v'==1 & family==0

    est sto nom_`v'`p'
    }
    }

    the model run only for the first variable and the first interaction terms, which is t_state##ib2.p_c with an error message:
    # invalid name
    # invalid name
    ib2.p_c invalid name
    r(7);

    I suspect that it was because I did wrong when define local for the interaction terms. I tested the following

    display `pv'
    invalid matrix stripe;
    t_state##ib2.p_c
    r(198);

    Thank you very much for the help!



  • #2
    The problem is arising in your -estimates store- command: a name like nom_b2t_state##ib2.p_c is not a valid name for a set of estimates. You will have to come up with some other naming scheme for those.

    Also, get rid of the -xi:- prefix. It does nothing unless there are some i.varnames in the $charm global macro. And it is unneeded, as Stata will handle this now with factor variable notation. See -help fvvarlist-. You should more or less forget you ever knew about -xi-. It is almost entirely obsolete--factor variable notation handling nearly everything you would do with -xi-, and handling it better and more simply. There are a few situations where you still need -xi-, but they are pretty exotic, or arise in connection with very old commands that do not support factor variable notation but have, themselves, been superseded by more modern commands that do.

    By the way, as a general debugging tip, when you encounter error messages arising inside a loop, it is hard to know which command is throwing the error. You can make that more readily visible by adding -set tracedepth 1- and -set trace on- at the top of the loop. This will lead to expanded output that lets you see how the commands look after macro expansion, and also indicate clearly where the error messages are coming from.

    In the future, when showing code, please use code delimiters so it comes out nicely aligned and easy to read. If you are not familiar with code delimiters, read Forum FAQ #12, or watch David Benson's video at https://youtu.be/bXfaRCAOPbI (which also shows how to use the -dataex- command for showing example data.)

    Added: by the way, your -foreach v of varlist...- loop lists b2 twice.

    Comment


    • #3
      Clyde,

      thank you very much! I am thinking about how to solve the problem about naming. Can I add a third set of local variables to name correctly for esto?

      Thank you so much for the tips! I will read more about each of them.

      Rui

      Comment


      • #4
        HI Clyde,

        I am trying to create 1,2,3 after `v' to distinguish whether the results were from ppost, postdum, or state

        I did this

        set tracedepth
        set trace on

        foreach v of varlist b2 b4 b8 b12 b16 {
        foreach p of local pv{
        drop i
        gen i=1
        replace i=i+1
        xi:svy: reg `v' `p' $charm if r_`v'==1 & married1==0

        est sto nom_`v'`i'

        }
        }

        the model is running, however the eststo name only gave me nom`v'1 for all of them. I probably put the replace i=i+1 at the wrong place. Can you help with it?

        Thanks a lot!

        Rui

        Comment


        • #5
          Right idea, but wrong implementation. Creating a variable named i is not going to give you a local macro i for use in naming. If you want to use numbers 1 through 3 for this, you can simplify it this way:

          Code:
          foreach v of varlist b2 b4 b8 b12 b16 {
              forvalues i = 1/3 {
                  local p: word `i' of  `pv' {
                  svy: reg `v' `p' $charm if r_`v'==1 & married1==0
                  est sto nom_`v'`i'
              }
          }
          Once again, please, in the future, show neatly aligned and indented code inside code delimiters, as previously requested.

          Comment


          • #6
            Clyde,

            after i added another "}" at the end of the program, it works! Thank you so much! Thanks for showing me what is a neatly aligned and indended code. I will watch the video you recommended.

            Thanks again for your help!

            Rui

            Comment


            • #7
              Hi Clyde,

              Thank you so much for your help yesterday. i ran the program again today. something weird happened.
              As I wrote yesterday, the program you provided seemed missing a "}" at the end and it did not run. After I added the }, the program started running but at the end it gave me error message:

              } is not a valid command name
              r(199);

              If i used interactive screen, the next program continued to run. I tried to figure out what happened and went back to you original program above with two "}"s instead of three "}"s, something weird happened, stata seemed to stop running and only record the lines I inputted. I tried to use control break to exit stata bit it did not exit at all. I suspected it was a loop problem but did not have to solve it.

              Can you help?

              Thanks a lot!

              Rui

              Comment


              • #8
                I have encountered similar weird behavior at times myself when using code copied from this Forum. I think that sometimes the Forum software inserts non-printing characters that it uses to control the layout of the text, and when those non-printing characters get copied into a do-file, Stata sees them even though our eyes don't. The result is usually that Stata considers the command invalid. Sometimes, but not invariably, you can fix this by copying the code from the Forum into a simple text editor, and save it as a plain text file. Then re-open it, copy the contents and past them back into the do-editor. Try running it from the do-editor again--it may work. If it does not, I don't know any alternative to closing out the do-editor, re-opening the do-editor to a new, empty file, and typing the code in manually.

                Comment


                • #9
                  Clyde,

                  Thank you very much! I will try that.

                  I did a trick before I saw your message, i use the dofile nostop option, which seems working.

                  I really appreciate your help!

                  Rui

                  Comment

                  Working...
                  X