Announcement

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

  • Nested foreach i of num loops and output files

    Hi STATA forums,

    I have a somewhat simple nested -foreach- loop. It is importing JSON files, reformatting them, and attempting to save as new stata datasets. The file formats of the incoming JSON are: 01_1.json, 01_2.json, 01_3.json, etc. First digit signifies state while the second digit signifies which batch of information is in each file. There are eight batches for each state. I've simplified the loop below to focus on just processing the first batch of files for states 6 8 and 9 (to test loop).

    The loop runs successfully, but I have the following issue:

    The file omits the i-th marker, instead saving several times over on (test as example) test_1_stata.dta instead of test1_1_stata.dta.


    The loop is as follows:

    Code:
    foreach i of num 6 8 9 {
    
    clear
    
    filefilter ".../0`i'_1.json" json.txt , from("}, {") to(/r) replace
    
    import delimited json.txt, delim("/t") stripquotes(no) clear
    moss v1, match(`"("[^"]+")"') regex
    
    drop if _pos8==.
    
    foreach i of num 1/9 {
    replace _match`i' = subinstr(_match`i', `"""', "", .)
    }
    
    drop v1 _count _pos1 _pos2 _pos3 _pos4 _pos5 _pos6 _pos7 _pos8 _pos9
    
    renvars, map(strtoname(@[1]))
    
    drop in 1
    
    destring Emp FrmJbLs state county, replace
    ren state state_fips
    ren county county_fips
    
    save "../test`i'_1_stata.dta", replace
    
    }
    So I would expect 3 files from the above loop name test6_1_stata.dta, test8_1_stata.dta, etc. However I am just getting test_1_stata.dta.

    I have omitted the path as for privacy but it is pointing to the correct location and is saving the improperly name file in that location.

    What I suspect to be the issue: my improper understanding of foreach i of num OR the second foreach is clearing the i-th placeholder?

    I've tried quite a few things but would appreciate any insight on the issue. I read Nick Cox's pub on \ vs / and was hoping that using / would solve the issue! Alas no luck.

    Using Stata 13 (MP).
    Last edited by Jeffery Sauer; 15 Jun 2018, 16:01.

  • #2
    SOLVED:

    I did not understand that re-using i as my increment placeholder would clear it! Thus I changed the nested loop to f instead of i and it is now working!

    Comment


    • #3
      Welcome to Statalist, and congratulations on your first posted answer - even if it was to your first posted question!

      Your use of CODE delimiters to display your code is appreciated (and somewhat rare among new posters), as is your providing the answer to the problem (rather than just saying that you solved it).

      Comment

      Working...
      X