Announcement

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

  • Skip commands in a loop

    Hello,

    I'm running the following loop:

    Code:
    cd "..."
    forval i = 1(1)742 {
        capture noisily import delimited `i'.txt, delimiter(space) clear
        rename (v1 v2 v3 v4 v5) (year month place dist state)
        save `i', replace    
    }
    Some numbers are missing in the range [1,742]. How can I tell stata to ignore the remaining commands when the first one is not working?

  • #2
    See
    Code:
    help continue

    Comment


    • #3
      You can use various commands to get a list of wanted files and loop over that. Or you can check whether each file exists within the loop.


      Code:
        capture confirm file `i'.txt
      and make the rest of the code conditional on

      Code:
      if _rc == 0 { 
      
      }

      Comment


      • #4
        Thank you, Hemanshu and Nick.

        Comment

        Working...
        X