Announcement

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

  • Simple loop (parallel with two lists of numbers)

    Hi All,

    I am new to loops, especially running them in parallel fashion. I want to change years in data files as well as number sets from files. I have no idea if I am doing this correctly.

    Here is my code example.

    local LIST 41 49 50 52 53 54 55 56 57 58 59 60

    set trace on

    forval i=0/4{
    foreach in local LIST {
    clear
    cd "C:\Users\jsco131\Documents\extracts\RMS\201`i ’"

    import delimited C:\Users\jsco131\Documents\extracts\RMS\201`i’\Mov ement_Files\5003_201`i’\50`LIST’_201`i’.tsv

    merge m:1 store_code_uc using "C:\Users\jsco131\Documents\extracts\RMS\201`i’\st ores_201`i’.dta"

    keep if _merge==3
    drop _merge
    save StoreData50`LIST’_201`i’.dta

    }

    }

    set trace off



    How do I fix this?

    Thank you in adavance.

    -Josh

  • #2
    tricky to really see what's up, but try this.

    You need a indicator for the foreach loop. I've used s.
    There were some spaces in some words, but that may be a copy/paste issues.
    Why "tsv" rather than "csv" ?
    I prefer -joinby- to merge m:1. It will only keep the matches as written and do the m:1 match (I think).

    Code:
    local LIST 41 49 50 52 53 54 55 56 57 58 59 60
    forval  i =0/4 {
         foreach s in local LIST {
         clear
         di "Outer loop = " `i'
         di "Inner loop = " `s'
         cd "C:\Users\jsco131\Documents\extracts\RMS\201`i ’"
         import delimited C:\Users\jsco131\Documents\extracts\RMS\201`i’\Movement_Files\5003_201`i’\50`s’_201`i’.csv 
         joinby store_code_uc using "C:\Users\jsco131\Documents\extracts\RMS\201`i’\stores_201`i’.dta" 
         drop _merge
         save StoreData50`s’_201`i’.dta
         }
    }

    Comment


    • #3
      First off, you have nested loops. not loops in parallel. As far as I can see the original code will fail as

      Code:
      foreach in local LIST
      should be more like

      Code:
      foreach s of local LIST
      There were two errors there and George Ford 's helpful rewriting corrected one.

      The line

      Code:
       cd "C:\Users\jsco131\Documents\extracts\RMS\201`i ’"
      could usefully be moved up out of the inner loop, as you need to cd 5 times (but not 60 times).

      Last edited by Nick Cox; 19 Oct 2021, 17:26.

      Comment


      • #4
        Why "tsv" rather than "csv" ?
        Some people use .txv as a filename extension for text files with tab-separated variables.

        Comment


        • #5
          Thank you all for your input!

          I tried writing the nested loops in George Ford 's style and change the inner loop to "foreach s of local LIST", but it currently won't change the directory, which I have never had a problem with. It also didn't matter whether I had it inside both loops or not.

          Any reason why this is occurring?

          Comment


          • #6
            before and after the cd command type pwd and see what happens. It will display the active directory.

            or just delete the cd line and add the directly to the save line (if that is why you cd in the first place).

            Comment

            Working...
            X