Announcement

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

  • Drop variables under certain conditions

    Dear All,

    I am facing difficulties in dropping observations of some 300 variables and 140 of them have a common suffix. All these 140 variables end by _xt and I want to drop observations for all the 300 variables if at least one the 140 variables ending by _xt has a value <= 5. The following command does not work : drop if *_xt <=5 .

    Thank you in advance!

    Last edited by Ina Diak; 19 Mar 2020, 14:13.

  • #2
    In Stata, you either drop observations or drop variables. There is no such thing as dropping observations of a particular variable only.

    Code:
    foreach var of varlist *{
        if regexm("`var'", "$_xt") & `var'<=0.5{
           drop `var'
        }
    }

    Comment


    • #3
      I think what is wanted is to drop observations where the minimum of the 140 *_xt variables is 5 or less. From that perspective then, the following example demonstrates the technique.
      Code:
      . set obs 1000
      number of observations (_N) was 0, now 1,000
      
      . set seed 666
      
      . forvalues i = 1/140  {
        2.         generate v`i'_xt = runiform(1,1000)
        3. }
      
      . egen todrop = rowmin(*_xt)
      
      . drop if todrop<=5
      (425 observations deleted)

      Comment


      • #4
        Originally posted by Andrew Musau View Post
        In Stata, you either drop observations or drop variables. There is no such thing as dropping observations of a particular variable only.

        Code:
        foreach var of varlist *{
        if regexm("`var'", "$_xt") & `var'<=0.5{
        drop `var'
        }
        }
        Thank you Andrew. Indeed I want to drop observations for all the variables in the dataset if one or more of the 140 ending by _xt has a value <= 5. I tried the command you shared but it does not work. I received the following message : type mismatch r(109)

        Thanks

        Comment


        • #5
          Then William Lisowski's solution should work if your variables ending in _xt are numerical. Your error suggests that you have string variables ending in _xt. If these are numerical variables, see

          Code:
          help destring
          Otherwise, here is a way to achieve what you want in the presence of both strings and numerical variables ending in _xt.

          Code:
          ds *_xt
          local xtvars=r(varlist)
          ds, has(type numeric)
          local nvars=r(varlist)
          local sxtvars: list xtvars-nvars
          local nxtvars: list xtvars-sxtvars
          egen todrop = rowmin(`nxtvars')
          drop if todrop<=5

          Comment


          • #6
            Originally posted by William Lisowski View Post
            I think what is wanted is to drop observations where the minimum of the 140 *_xt variables is 5 or less. From that perspective then, the following example demonstrates the technique.
            Code:
            . set obs 1000
            number of observations (_N) was 0, now 1,000
            
            . set seed 666
            
            . forvalues i = 1/140 {
            2. generate v`i'_xt = runiform(1,1000)
            3. }
            
            . egen todrop = rowmin(*_xt)
            
            . drop if todrop<=5
            (425 observations deleted)
            Thank you William,
            Instead, Indeed I want to drop observations for all the variables in the dataset if one or more of the 140 ending by _xt has a value <= 5.

            Comment


            • #7
              Originally posted by Andrew Musau View Post
              Then William Lisowski's solution should work if your variables ending in _xt are numerical. Your error suggests that you have string variables ending in _xt. If these are numerical variables, see

              Code:
              help destring
              Otherwise, here is a way to achieve what you want in the presence of both strings and numerical variables ending in _xt.

              Code:
              ds *_xt
              local xtvars=r(varlist)
              ds, has(type numeric)
              local nvars=r(varlist)
              local sxtvars: list xtvars-nvars
              local nxtvars: list xtvars-sxtvars
              egen todrop = rowmin(`nxtvars')
              drop if todrop<=5
              Thank you Andrew, this code works perfectly.

              Comment


              • #8
                Hi Ina, thanks for the closure. I realized that the macro subtraction is not necessary. For future reference, the following is sufficient.

                Code:
                ds *_xt, has(type numeric)
                local nxtvars=r(varlist)
                egen todrop = rowmin(`nxtvars')
                drop if todrop<=5

                Comment


                • #9
                  This should work too:

                  Code:
                  ds *_xt, has(type numeric)
                  egen todrop = rowmin(`r(varlist)')
                  drop if todrop<=5

                  Comment

                  Working...
                  X