Announcement

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

  • Doubts regarding keep and drop

    Hello folks.

    I wish to drop all variables which value in 1st row is 5 times or greater than the value in the 1st row of the variable newvar.

    How can i do that, please?

    Thanks in advance.

  • #2
    Code:
    foreach v of varlist * {
      drop `v' if (`v'[1]  >= (5*newvar[1]))
    }

    Comment


    • #3
      @Mike Lacy’s code drops observations. That may be what is really wanted, but the question says that the OP wants to drop variables.

      The code should still be legal with string variables, by accident as it were.

      Comment


      • #4
        Nick-- I think I am suggesting to drop variables, i.e. -drop `v'-.

        Comment


        • #5
          Thanks for your message, Mike.
          However, the code is not working. I receive the message "invalid syntax".
          I'm using exactly this:

          Code:
          foreach v of "dataset por uf" * {
            drop `v' if (`v'[1]  >= (5*hompcmRS[1]))
          }
          I also tried:

          Code:
          foreach v of varlist * {
            drop `v' if (`v'[1]  >= (5*hompcmRS[1]))
          }
          And:

          Code:
          foreach v of "dataset por uf.dta" * {
            drop `v' if (`v'[1]  >= (5*hompcmRS[1]))
          }
          None is working. Am I doing somehing wrong?

          Comment


          • #6
            Your code omitted "varlist" that was in my suggested code (see -help foreach-) and included a file name, which I didn't suggest and which is not part of the foreach command. Open your data file and type *exactly* the code I listed above.

            Comment


            • #7
              My newvar is, actually, hompcmRS. This is the message I receive:


              Code:
              . foreach v of varlist * {
                2. 
              .   drop `v' if (`v'[1]  >= (5*hompcmRS[1]))
                3. 
              . }
              invalid syntax
              r(198);

              Comment


              • #8
                I would also add that saying something is not "working" without any detail doesn't help us help you There are many ways some code might not work, and we can't see your screen to know which one it was. A wild guess on my part would be that you didn't open (-use-) your data file before applying the command. The second command you show should work fine if you have opened the data file, but perhaps there is some error I'm not seeing.

                Comment


                • #9
                  Sorry, but I misspoke. I should follow a good role model: "We must not be hasty. I have become too hot. I must cool myself and think."

                  Backing up, there are two and only two possibilities. You can drop entire variables (columns in the dataset) or you can drop entire observations (rows in the dataset). You can't do both at the same time -- meaning, in the same command. Nothing stops two or more commands that combine different kinds of drop.

                  If the desire is to drop an entire variable given the stated condition, this could be the syntax. To remove an irrelevancy, I first condition on variables being numeric.

                  Code:
                  ds, has(type numeric) 
                  local numvars `r(varlist)' 
                  
                  foreach v of local numvars { 
                        if (`v'[1] >= newvar[1]) drop `v' 
                  }
                  This conditional is completely different from @Mike Lacy's code, which is not in fact legal.

                  If the desire is to drop observations you could do this -- it's iegal -- but most unlikely to be what anyone wants. I assume the same selection of numeric variables only

                  Code:
                  foreeach v of local numvars { 
                        drop if (`v'[1] >= newvar[1]) 
                  }
                  However, this is unlikely to be what anyone wants, and calm analysis shows that the consequence is either to drop everything or to drop nothing.


                  Comment


                  • #10
                    I see a problem here - when `v' is "newvar", it will be dropped and the code will break and won't check any further variables; I also note that I prefer, when using "if ..." to use curly braces to keep things clearer

                    Comment


                    • #11
                      Thank you, everyone. Rich, you are right! Now Nick's code works!
                      Last edited by Edison Jr; 22 Sep 2022, 14:49.

                      Comment

                      Working...
                      X