Announcement

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

  • Read Stata file using part of file name only

    Hello,

    I have a large number of datasets that have very long filenames but each have a unique string within the filename (in this instance "country"). The files are not mine so I cannot change the names of the files, but would like to read in all the files into Stata based on a part of the filename, for example a file name could be:

    "file 1 project 102 temporary data sweden individualpatientdata.dta"

    or

    "file 8 project 103 temporary data norway individualpatientdata.dta"

    Now, could I use the string part that is based on country to read in the dataset? Obviously, the following wouldn't work:

    .use "$data/*sweden*.dta , clear
    .use "$data/*norway*.dta , clear

    but that is essentially what I would like to do. All the files sit on separate drive (F) so I could store the file names in a local like this,

    .local myfiles: dir "F:/" files "*.dta"

    but then I do not know how to continue.

    Any help would be much appreciated!

    Thanks,

    Raoul

  • #2
    Code:
    local COUNTRY "sweden"
    
    mata : st_local( "fn", dir("$data", "files", "*`COUNTRY'*" , 1 ) )
    use "`fn'" , clear
    ref:
    Code:
    help mf_dir
    Last edited by Bjarte Aagnes; 16 Feb 2019, 09:12.

    Comment


    • #3
      Wow, thank you so much Bjarte, clearly can see the power of Mata now!

      Comment


      • #4
        fs (SSC) will return one filename if the wildcard is specific enough.

        Code:
        . fs au*.dta
        auto.dta
        
        . ret li
        
        macros:
                      r(files) : ""auto.dta" "

        Comment

        Working...
        X