Announcement

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

  • How to use and save hundreds of files using loops

    Hi Stata users,

    I am trying to use, split and save hundreds of files with the following names below. The numbers at the start of the filenames are in the interval of 50 from 1 to 5150.

    1-50_ProductAffected
    51-100_ProductAffected
    101-150_ProductAffected
    151-200_ProductAffected
    ...
    5101-5150_ProductAffected

    Code:
    forval c=1(50)51{
    forval i=50(50)100{
    use "`c'-`i'_ProductAffected.dta",  clear
    
    //Split AffectedProducts//
    generate id = _n
    split HScode, p(,)
    drop HScode
    reshape long HScode, i(id)
    keep if HScode!=""
    drop id _j
    
    gen H2=substr(HScode, 1, 2)
    label var H2 "HS2 codes of HScode varialble"
    save "Split\\`c'-`i'_ProductAffected.dta", replace 
    }
    }

    Once the variable AffectedProducts is split, the dataset will be saved in it's original filename (e.g., 1_50ProductAffected) but different folder. When I run the code above, only the first filename (i.e., 1-50_ProductAffected) is correctly used, split, and saved. I can no longer use the next filename (i.e. 51-100_ProductAffected). Instead what appears is the error message: file 1-100_ProductAffected.dta not found

    Definitely, my looping fails to correctly capture the naming pattern of the filenames. But I don't know how to correct it. All help is welcome.

    Thank you!



  • #2
    one way to do this,
    Code:
    forval i = 50(50)5100 {
    
    local a = `i' + 1
    local b = `i' + 50
    
    use "`a'-`b'_ProductAffected", clear
    
    ...
    
    }

    Comment


    • #3
      You've written in #1 two nested loops which should run jointly over 4 possibilities, c being 1 and 51 and i being 50 and 100, so (1 50) (1 100) (51 50) (51 100), which is not what you want.

      You want two loops in parallel, except that means just one loop as in #2, as you can get the second number of each pair inside the loop.

      Comment


      • #4
        Originally posted by Øyvind Snilsberg View Post
        one way to do this,
        Code:
        forval i = 50(50)5100 {
        
        local a = `i' + 1
        local b = `i' + 50
        
        use "`a'-`b'_ProductAffected", clear
        
        ...
        
        }
        Thank you Øyvind! Your code works and saves me a lot of time.

        Comment


        • #5
          Originally posted by Nick Cox View Post
          You've written in #1 two nested loops which should run jointly over 4 possibilities, c being 1 and 51 and i being 50 and 100, so (1 50) (1 100) (51 50) (51 100), which is not what you want.

          You want two loops in parallel, except that means just one loop as in #2, as you can get the second number of each pair inside the loop.
          Thank you, Nick, for explaining what's wrong with my code. Very helpful as always.

          Comment

          Working...
          X