Announcement

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

  • Dropping variables by certain words in the label*

    Hi,

    This is a picture of my dataset that contains many variables containing terms e.g. 'index'. Is it possible to batch delete them?

    Click image for larger version

Name:	Screenshot 2020-09-22 170458.jpg
Views:	1
Size:	140.6 KB
ID:	1573877
    Last edited by Sonnen Blume; 22 Sep 2020, 15:18.

  • #2
    Code:
    foreach v of varlist _all {
        if strpos(`"`:var label `v''"', "index") {
            drop `v'
        }
    }
    In the future, when showing data examples, please use the -dataex- command to do so. If you are running version 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    Comment


    • #3
      Originally posted by Clyde Schechter View Post
      Code:
      foreach v of varlist _all {
      if strpos(`"`:var label `v''"', "index") {
      drop `v'
      }
      }
      In the future, when showing data examples, please use the -dataex- command to do so. If you are running version 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.
      Thanks a lot Clyde for the code. It works perfectly. Will this command work for more than one term at the same time?

      I usually use dataex when necessary to explain the situation. In this case, it seemed that showing the picture will make better sense. Thanks for the recommendation.

      Comment


      • #4
        First, it is unclear what you mean by "more than one term at the same time." Do you want to drop the variable if it includes both "index" and, say, "history," or do you want too drop the variable if it includes either one.

        Either way, the code could be modified to do it by addming more -strpos()- terms to the -if- condition, adjoined with either & (for both) or | (for either).

        Added, it just hit me that the original problem can also be done a bit more simply:

        Code:
        ds, has(varlabel *index*)
        drop `r(varlist)'
        This approach can be extend also to cover dropping when there are several words that trigger dropping if any of them is present:

        Code:
        ds, has(varlabel *index* *history*)
        drop `r(varlist)'
        But this approach would be more difficult to extend to dropping when there are several words that must all be present in the variable label. In fact, doing that would be no simpler than modifying the approach in #2.
        Last edited by Clyde Schechter; 22 Sep 2020, 17:12.

        Comment


        • #5
          Originally posted by Clyde Schechter View Post
          First, it is unclear what you mean by "more than one term at the same time." Do you want to drop the variable if it includes both "index" and, say, "history," or do you want too drop the variable if it includes either one.

          Either way, the code could be modified to do it by addming more -strpos()- terms to the -if- condition, adjoined with either & (for both) or | (for either).

          Added, it just hit me that the original problem can also be done a bit more simply:

          Code:
          ds, has(varlabel *index*)
          drop `r(varlist)'
          This approach can be extend also to cover dropping when there are several words that trigger dropping if any of them is present:

          Code:
          ds, has(varlabel *index* *history*)
          drop `r(varlist)'
          But this approach would be more difficult to extend to dropping when there are several words that must all be present in the variable label. In fact, doing that would be no simpler than modifying the approach in #2.
          This is wonderful!

          Code:
           
           ds, has(varlabel *index* *history*) drop `r(varlist)'
          does what I meant by 'several words'.

          Comment


          • #6
            See also findname (Stata Journal) which has a similar but not identical syntax for this problem, but in total more hooks.

            Comment

            Working...
            X