Announcement

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

  • Forval Loops Breaks because of irregularly spaced values

    I want to execute the following loop
    Code:
    forval i = 1/500 {
    qui sum Ri if PF`i' > 10
    loc LF = r(mean)
    ...
    ...
    ...
    }
    The problem arises when PF`i' is not regularly spaced. How can I force the loop to continue and ignore if PF`i' has missing sequential number i.e. if PF1 and PF2 are followed by PF4, the loop will stop.
    Regards
    --------------------------------------------------
    Attaullah Shah, PhD.
    Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
    FinTechProfessor.com
    https://asdocx.com
    Check out my asdoc program, which sends outputs to MS Word.
    For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

  • #2
    You need to be more specific about what exactly you want to do if PF<i> is missing, but here is some technique
    Code:
    forv i = 1/500 {
    cap conf v PF`i'
    if (_rc)  continue
    qui sum Ri if PF`i' > 10
    ...
    ...
    ...
    }
    Best
    Daniel

    Comment


    • #3
      If the PF* variables are consecutive in the dataset, you can use -foreach- instead. If needed, you can make them consecutive with -order-:
      Code:
      order PF* , sequential
      foreach i of varlist PF1-PF500 {
      qui sum Ri if `i' > 10
      loc LF = r(mean)
      ...
      }

      Comment


      • #4
        Daniel you are my hero. Thank you Daniel and Svend Juul for your excellent replies. I made a mistake in my post. Actually, I wanted to ask if a variable is not sequentially placed, then how the forval loop will continue. So my request should read
        Code:
        forval i = 1/500 {
        qui sum Ri if PF==`i'
        loc LF = r(mean)
        ...
        ...
        ...
        }
        Last edited by Attaullah Shah; 25 Oct 2014, 07:40.
        Regards
        --------------------------------------------------
        Attaullah Shah, PhD.
        Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
        FinTechProfessor.com
        https://asdocx.com
        Check out my asdoc program, which sends outputs to MS Word.
        For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

        Comment


        • #5
          On a different note:

          If all you want after summarize is the mean, use the meanonly option. Even if you want some other stuff, still consider the meanonly option.

          See e.g. http://www.stata-journal.com/sjpdf.h...iclenum=st0135

          Comment


          • #6
            If the problem isn't absence of variables in a sequence, but the ordering of variables numbered sequentally, there is no problem. PF427 is PF427 no matter where in the list of variables it is located.

            To Nick: I avoid telling (when teaching) other people about the meanonly option; its name is confusing, and quietly can be used generally. meanonly saves computer, time, right, but nowadays brain time is much more costly than computer time.

            Comment


            • #7
              Hi Attaullah,

              You can use -levelsof- to capture distinct values of the variable PF and then run the loop using -foreach- instead of -forvalue- as follows:

              Code:
              levelsof PF, local(PFlevels)
              foreach i of local PFlevels {
                  qui sum Ri if PF==`i', meanonly
                  loc LF = r(mean)
                  ...
                  ...
                  ...
              }

              Comment


              • #8
                Svend: I take your point. Attaullah has elsewhere made it evident that he is interested in learning to program in Stata, and many others are too.

                In the 2007 paper cited I suggested a different name.

                Comment


                • #9
                  Attuallah: Re-reading your post #4: You write about the sequential placement of variables, but in the code you have a single variable, PF, which can take different values.

                  Comment


                  • #10
                    Thanks for your replies.
                    To Abraham: I have used levelsof previously, but it considerably slows things down.
                    To Nick : Yes I do use meanonly option, in the example I just wanted to fill space with some stata command to complete the example of loop
                    To Svend : You are right, I did not clarify my point enough, I am interested in using distinct values of the variable PF, not different variables.

                    So my problem remains somehow unresolved.
                    Regards
                    --------------------------------------------------
                    Attaullah Shah, PhD.
                    Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
                    FinTechProfessor.com
                    https://asdocx.com
                    Check out my asdoc program, which sends outputs to MS Word.
                    For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

                    Comment


                    • #11
                      Attuallah: Didn't Abraham's suggestion in post #7 do what you want?

                      Comment

                      Working...
                      X