Announcement

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

  • Find datasets with if argument

    Hi,

    I have around 2000 datasets and I would like to find the ones in which a certain variable has certain observations.
    In particular, I want to find and re-save the datasets in which the variable "grade" takes the string letters: AB, BA or B
    For example:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float id str2 grade
     1 "AB"
     2 "BA"
     3 "BA"
     4 "AB"
     5 "B"
     6 "AB"
     7 "B"
     8 "BA"
     9 "B"
    10 "B"
    end
    The thing is that my "grade" variable in some of the datasets does not take the values AB, BA and B (like below) and I would like to skip over those datasets.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(id grade)
     1 4
     2 5
     3 2
     4 3
     5 5
     6 1
     7 5
     8 3
     9 3
    10 2
    end
    I thought about using levelsof but I do not know how to implement it in this case.
    How can I do that? Is there any other way?

    Best

  • #2
    Let me start by assuming that you already have a list of the data set filenames stored in a local macro called filenames. (If you don't have such a list and don't know how to create one, post back.)

    Code:
    foreach f of local filenames {
        use`"`f'"', clear
        capture confirm string var grade, exact
        if c(rc) == 0 {
            capture assert inlist(grade, "AB", "BA", "B")
            if c(rc) == 0 {
                display `"File `f' has grade = "AB", "BA", or "B""'
                // CODE TO DO WHATEVER YOU WANT WITH SUCH FILES GOES HERE
            }
        }
    }

    Comment


    • #3
      Originally posted by Clyde Schechter View Post
      Let me start by assuming that you already have a list of the data set filenames stored in a local macro called filenames. (If you don't have such a list and don't know how to create one, post back.)

      Code:
      foreach f of local filenames {
      use`"`f'"', clear
      capture confirm string var grade, exact
      if c(rc) == 0 {
      capture assert inlist(grade, "AB", "BA", "B")
      if c(rc) == 0 {
      display `"File `f' has grade = "AB", "BA", or "B""'
      // CODE TO DO WHATEVER YOU WANT WITH SUCH FILES GOES HERE
      }
      }
      }
      Thanks Clyde, I also wrote the following code which also works but yours is much more elegant and bulletproof. Thanks!

      Code:
      foreach x in $filenames {
          use "`x'", clear
      cap{
          count if grade == "AB" | grade == "BA"
          if r(N) != 0 {
              save "`x'", replace
              }
      }
      }

      Comment


      • #4
        Originally posted by Clyde Schechter View Post
        Let me start by assuming that you already have a list of the data set filenames stored in a local macro called filenames. (If you don't have such a list and don't know how to create one, post back.)

        Code:
        foreach f of local filenames {
        use`"`f'"', clear
        capture confirm string var grade, exact
        if c(rc) == 0 {
        capture assert inlist(grade, "AB", "BA", "B")
        if c(rc) == 0 {
        display `"File `f' has grade = "AB", "BA", or "B""'
        // CODE TO DO WHATEVER YOU WANT WITH SUCH FILES GOES HERE
        }
        }
        }
        About looping over all the files that you mentioned, I have encountered a problem. I want to run the code over all the datasets in a certain folder and I use the following code:

        Code:
        local filelist : dir "C:\UsersData\Working Data\Grades\Working\courses" files "*.dta"
        
        foreach x of local filelist {
            use "`x'", clear
            gen x=_n
            
        }
        But I get the error file aada01.dta not found.
        This is because the name of the fist file is actually "AADA01.dta" and not "aada01.dta". How can I fix this?

        Comment


        • #5
          I take it you are working on a Mac or a Unix system, as in Windows filenames are not case sensitive.

          Code:
          local filelist : dir "C:\UsersData\Working Data\Grades\Working\courses" files "*.dta", respectcase

          Comment


          • #6
            Originally posted by Clyde Schechter View Post
            I take it you are working on a Mac or a Unix system, as in Windows filenames are not case sensitive.

            Code:
            local filelist : dir "C:\UsersData\Working Data\Grades\Working\courses" files "*.dta", respectcase
            Works great, thanks!
            Last edited by Neg Kha; 05 Jan 2024, 03:14.

            Comment

            Working...
            X