Announcement

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

  • How to loop over entire variable in groups of 5?

    Hi,
    I wish to run some line graphs using sepscatter to identify outliers. I want to run this for every location number in a variable entitled "location". My sample code is below:

    ```
    sepscatter revenue monthly if inlist(location, 233, 234, 235, 236, 239, 240), sep(location) recast(line) lc(blue orange black red green purple grey)

    graph export "C:\Users\jscot\Desktop\graphs", as(png) replace

    ```

    How would I either 1) loop this for every location, or 2) loop this but in groups of 5 so that I can have less graphs?

    I am okay with either method. The self help docs I found online didn't seem to be what I was looking for, so I figure I would go to the experts.

    Thank you!

  • #2
    sepscatter is from SSC. Your example locations have a gap with no 237 or 238. So work with a grouping variable that is complete within its range.

    Code:
    egen group = group(location) 
    
    gen group2 = ceil(group/5) 
    
    su group2, meanonly 
    
    local graphs 
    
    forval g = 1/`r(max)' { 
         sepscatter revenue monthly if group2 == `g', sep(location) recast(line) lc(blue orange black red green purple grey) name(G`g') 
         graph export G`g', as(png) 
         local graphs `graphs' G`g' 
    } 
    
    * possibly ridiculous 
    graph combine `graphs'

    Comment


    • #3
      Thank you!! This worked incredibly well, and was exactly what I was looking for!

      Comment

      Working...
      X