Announcement

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

  • Setting a variable equal to a local macro in a foreach loop

    Code:
    quietly{
        levelsof id, clean local(url)
        foreach x of local url{
            copy "http://www.ufcstats.com/fighter-details/`x'" "fighter`x'.txt", replace
        }
    }
    //generate clean career data for each player
    quietly{
        foreach x of local url{
            clear
            import delimited using fighter`x'.txt, rowr(139:193)
        
            g careerStats = strmatch(v1, "*<*")
            drop if careerStats == 1
            replace careerStats = mod(_n,2) == 1
            keep if careerStats == 1
            g str id = `x'
            g slpm = v1
            g strAcc = v1[_n+1]
            g sapm = v1[_n+2]
            g strDef = v1[_n+3]
            g tdAvg = v1[_n+4]
            g tdAcc = v1[_n+5]
            g tdDef = v1[_n+6]
            g subAvg = v1[_n+7]
            drop v1 careerStats
            keep in 1
        
            save `x', replace
        
        }
    }
    The code pulls data from a list of urls by cycling through unique ids for each page. I need the id for each specific webpage to be in each new data set I create so that I can append them to the larger data set I have created.
    I'm hitting an error when I get to the line: g str id = `x'. Stata is shooting back an error(198) when I attempt to generate the variable.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str16 fighter
    "93fe7332d16c6ad9"
    "15df64c02b6b0fde"
    "b361180739bed4b0"
    "2f5cbecbbe18bac4"
    "c0ed7b208197e8de"
    "5140122c3eecd307"
    "c9f6385af6df66d7"
    "aa6e591c2a2cdecd"
    "7279654c7674cd24"
    This is a portion of the ids that the program runs through. Thank you ahead of time!

  • #2
    Code:
    gen str id = `"`x'"'

    Comment

    Working...
    X