Announcement

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

  • Drop all string variables

    How can I drop all string variables from a dataset in one go, i.e. without typing "drop stringvariable1 stringvariable2..."?

    Background: I am looping over datasets so I need STATA to recognize the strings itself in each of the different datasets and then drop them.

    I know it's a very basic question but I could not find the answer in the web. I look forward to your comments!

  • #2
    Code:
    ds, has(type string)
    drop `r(varlist)'

    Comment


    • #3
      Thank you for your quick and helpful response!

      Comment


      • #4
        An alternative is:
        Code:
          
        findname, type(string)
        local string `r(varlist)'
        foreach i of local string {
        drop `i'
        }
        This is a bit longer, but compared to the code provided by Clyde Schechter, it gives no error message when the dataset does not contain any string variable. This can be useful when one wants to loop over several datasets and not all datasets contain strings.

        Comment


        • #5
          Paul: You don't need a loop there. You can just condition on the list of names being populated.

          Code:
          findname, type(string)
          if "`r(varlist)'" != "" drop `r(varlist)'
          Note: findname is from the Stata Journal and must be installed before it can be used. Please explain the origin of user-written commands you refer to. (FAQ Advice #12).

          Comment

          Working...
          X