Announcement

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

  • Code works on its own but not in a for loop

    I'm trying to do some data scraping from a bunch of related websites (they are different NFL team rosters from different years). My code works if I run it all separately, but when I try to use a for loop, it breaks in random places. Can anyone see the issue with this code?

    Code:
    forvalues i=1976/2019 {
        foreach var in ARI ATL BAC BAL BUF CAR CIN CLE DAL DEN DET GB HOO HOU IND JAC KC LAC LAA LAR MIA MIN NE NO NYG NYJ OAK PHI PHO SD SF SEA SLC STL TB TEN WAS {
            clear
            readhtmltable https://www.justsportsstats.com/footballroster.php?team=`var'&year=`i'
            gen year = `i'
            save "/Desktop/`i' `var' rosters (QB).dta", replace
        }
    }
    The error is an error with the user-generated -readhtmltable- command that says that it can't read the table for a given webpage...but then when I try to do that webpage not in the for-loop, it works.
    Last edited by Noah Spencer; 20 Jan 2020, 18:57.

  • #2
    I would guess that you are running the code a line at a time from a do-file editor window. If so, the definition of the local macros is not visible to the command that fails. The code must be run as a single chunk.

    Comment


    • #3
      If you haven't made the error Nick suggests, then you may want to set trace on and run it again. This will let you see how the macros are being interpreted.

      Comment


      • #4
        I was copying and pasting the whole chunk into the command box and it wasn't working, but when I tried highlighting it and 'do'ing it with the keyboard shortcut, it worked. Thanks for the help Nick and the suggestion Phil!

        Comment

        Working...
        X