Announcement

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

  • Looping and modifying a line row by row

    Hello, I am new to Stata's syntax and cannot figure out the syntax to do what I would like:

    Let's say that my chart looks like:

    var1 var2 var3

    1.3 4.5 3.3 (row 1)
    3.3 2.3 3.6 (row 2)
    4.5 1.1 2.2 (row 3)


    I would like to run a command that I wrote but for all values except each row (that is, I would like to run my program with just row 2+3, 1+3, and 1+2 to give 3 different outputs).



    I was wondering how I can make a for loop from say i= 1 to # of rows and within the for loop:

    delete row[i]
    run the program, give output
    insert row[i]


    I cannot seem to find anything online that seems to fit. Thank you very much!








  • #2
    Rather than delete and re-insert rows, why not just add an [if] option to your command's syntax? See
    Code:
    help syntax
    and
    Code:
    help mark
    as to how.

    Then you could do something like
    Code:
    forvalues row = 1/`=_N' {
        mycommand if _n != `row'
    }
    Here's an example with a command that has such an [if] option for illustration:

    .ÿsysuseÿauto
    (1978ÿAutomobileÿData)

    .ÿkeepÿinÿ1/3
    (71ÿobservationsÿdeleted)

    .ÿforvaluesÿrowÿ=ÿ1/`=_N'ÿ{
    ÿÿ2.ÿsummarizeÿpriceÿifÿ_nÿ!=ÿ`row'
    ÿÿ3.ÿ}

    ÿÿÿÿVariableÿ|ÿÿÿÿÿÿÿÿObsÿÿÿÿÿÿÿÿMeanÿÿÿÿStd.ÿDev.ÿÿÿÿÿÿÿMinÿÿÿÿÿÿÿÿMax
    -------------+---------------------------------------------------------
    ÿÿÿÿÿÿÿpriceÿ|ÿÿÿÿÿÿÿÿÿÿ2ÿÿÿÿÿÿÿÿ4274ÿÿÿÿ671.7514ÿÿÿÿÿÿÿ3799ÿÿÿÿÿÿÿ4749

    ÿÿÿÿVariableÿ|ÿÿÿÿÿÿÿÿObsÿÿÿÿÿÿÿÿMeanÿÿÿÿStd.ÿDev.ÿÿÿÿÿÿÿMinÿÿÿÿÿÿÿÿMax
    -------------+---------------------------------------------------------
    ÿÿÿÿÿÿÿpriceÿ|ÿÿÿÿÿÿÿÿÿÿ2ÿÿÿÿÿÿÿÿ3949ÿÿÿÿÿ212.132ÿÿÿÿÿÿÿ3799ÿÿÿÿÿÿÿ4099

    ÿÿÿÿVariableÿ|ÿÿÿÿÿÿÿÿObsÿÿÿÿÿÿÿÿMeanÿÿÿÿStd.ÿDev.ÿÿÿÿÿÿÿMinÿÿÿÿÿÿÿÿMax
    -------------+---------------------------------------------------------
    ÿÿÿÿÿÿÿpriceÿ|ÿÿÿÿÿÿÿÿÿÿ2ÿÿÿÿÿÿÿÿ4424ÿÿÿÿ459.6194ÿÿÿÿÿÿÿ4099ÿÿÿÿÿÿÿ4749

    .

    Comment


    • #3
      Thank you. It is not applying to my code, because my command is using multiple variables at once. For example it is not only using price, it is taking into account other variables in the same row.

      Comment


      • #4
        That sample code seems to summarize everything except the row in question. However, I was wondering if there was a way to temporarily remove that row instead? I would like to remove the row entirely and run the command instead of the command running everything except for the row.

        My command is complex and goes through 4 different variables and 100 different rows so removing the row on each loop seems to be the way it would best work. Thank you for your reply!

        Comment


        • #5
          Originally posted by Dave Power View Post
          Thank you. It is not applying to my code, because my command is using multiple variables at once. For example it is not only using price, it is taking into account other variables in the same row.
          There is no limitation on the number of variables used at once, and there is no limit on the number of rows. For example (using another command that has the [if] option), you can
          Code:
          forvalues row = 1/100 {
              regress var1 var2 var3 var4 if _n != `row'
          }
          I assure you that this will be much faster and more straightforward to implement than deleting and restoring rows of data 100 times.

          Comment


          • #6
            Originally posted by Dave Power View Post
            That sample code seems to summarize everything except the row in question. However, I was wondering if there was a way to temporarily remove that row instead? I would like to remove the row entirely and run the command instead of the command running everything except for the row.
            I don't see the practical difference in results between physically deleting the row and running the command and using the two suggestions above (an [if] option coupled with marksample); they would effectively give you the same results, the latter in a more straightforward manner and with less disc/cache I/O.

            But, if you insist, you could do what you want in a couple of ways.
            Code:
            forvalues row = 1/`=_N' {
                preserve
                drop in `row'
                mycommand
                restore
            }
            and nearly equivalently
            Code:
            tempfile tmpfil0
            quietly save `tmpfil0'
            forvalues row = 1/`=_N' {
                drop in `row'
                mycommand
                use `tmpfil0', clear
            }

            Comment


            • #7
              This topic was cross-posted on Stack Overflow at http://stackoverflow.com/questions/4...ine-row-by-row.

              Please review the Statalist FAQ linked to from the top of the page, as well as from the Advice on Posting link on the page you used to create your post. Note especially

              8. May I cross-post to other forums?



              People posting on Statalist may also post the same question on other listservers or in web forums. There is absolutely no rule against doing that.

              But if you do post elsewhere, we ask that you provide cross-references in URL form to searchable archives. That way, people interested in your question can quickly check what has been said elsewhere and avoid posting similar comments. Being open about cross-posting saves everyone time.

              If your question was answered well elsewhere, please post a cross-reference to that answer on Statalist.
              Reviewing the FAQ will also disclose to you that this post would have appropriately been posted in Statalist's General Forum, where it would have attracted a larger audience, rather than in the Mata Forum, which is dedicated to the more particular tools in Mata, which is of less general interest.
              Last edited by William Lisowski; 24 Mar 2017, 09:52.

              Comment

              Working...
              X