Announcement

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

  • If file name does not exist, jump over it

    Hi,

    I have around 2000 files and I want to loop over them. The problem is that some of those file names do not exist. For the ones that do exist, I want to save them as a Stata dataset. Regarding the ones that do not exist, I do not want anything to happen.

    Code:
    foreach x in $c {
        di "`x'"
        cap: import delimited using "C:/Users/39349/Documents/Courses/`x'", clear
        if _rc!=0 {
        display "file exists"
        save "C:/Users/39349/Documents/Courses- Stata/`x'", replace
        }
        else if _rc==0 {
        display "file does not exist"
        exit
        }
    
    }
    c is a global for all the names.
    If I run the mentioned code, it does not jump over the files that do not exist an it creates new Stata files with those names.
    How should I fix this?

    Best

  • #2
    Check out the file subcommand of -capture- (help capture). e.g.,

    Code:
    loc path  "C:/Users/39349/Documents/Courses/"
    foreach x of global c {
        di "`x'"
        cap confirm file `"`path'/`x'"'
        di _rc
        if !rc {
            di `"`x' found"'
            import delimited using `"`path'/`x'"', clear
           ***etc
        }
    }
    Last edited by eric_a_booth; 26 Dec 2023, 06:29.
    Eric A. Booth | Senior Director of Research | Far Harbor | Austin TX

    Comment


    • #3
      Originally posted by eric_a_booth View Post
      Check out the file subcommand of -capture- (help capture). e.g.,

      Code:
      loc path "C:/Users/39349/Documents/Courses/"
      foreach x of global c {
      di "`x'"
      cap confirm file `"`path'/`x'"'
      di _rc
      if !rc {
      di `"`x' found"'
      import delimited using `"`path'/`x'"', clear
      ***etc
      }
      }
      Hi,

      Thanks for the help.
      Is this what you had in mind?

      Code:
      global c AADA01    AADA05    AADA20    AADA25    AAHA20    AAHA20    AAHA20        AAHA60    AAHF01    AAHF10    AAHN02    AAHN15    AAHN25    AAHN40    AAHN45     AAKN20    AAMN01    ABAN06    ABAN11    ABAN15    ABFF01    ABKF10    ABKF10    ABMM01    ABMM01    ABMM02    ABMM03    ABMM08    ABMM50    ABMM55    ABMM70    ABVA05    AEBF10    AEBF15    AEBF25    AEBF30    AEBF30    AEBN10    AFOA15    AFOA20    AFOA25    AFON01    AFON20    AFON25    AFON30    AFON30    AKSA21    ALSM21    ARKA21    ARKN09    ARKN15    ASBF05    ASBN02    ASBN06    ASBN31    ASBN41    ASBN45    ASEN01    
      
      loc path  "C:/Users/39349/Documents/AF/Courses/Detailed"
      
      foreach x in $c {
          di "`x'"
          cap confirm file `"`path'/`x'"'
          di _rc
          if _rc!=0  {
          display "`x' exists"
          import delimited using `"`path'/`x'"', clear
          save "C:/Users/39349/Documents/AF/Courses- Stata/Detailed/`x'", replace
          }
      
      }
      But I get this error:
      file C:/Users/39349/Documents/AF/Courses/Detailed/AADA01.csv not found

      Because AADA01 is one of the files that does not exist.

      Comment


      • #4
        The display command should always work, so after that the return code is always 0. Therefore testing for the return code doesn’t filter out any problem cases. Copying the return code to a local macro and testing that would be a work-around.

        Comment

        Working...
        X