Announcement

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

  • foreach v outputting empty results

    I'm attempting to loop through results to output a table for each drug (id_*) then re-import the table back to STATA, and finally, strip the table to only what I need (e.g., value on 1) and append them together so I can sort the list and review. Unfortunately, I hit a snag on something and receiving an error code somewhere in my process. I believe it is in part 2 since part 1 runs fine. I think the issue is how I can generate an empty set for anything that doesn't have results. This is an abbreviated example since I actually have over 500 medications that I am trying to loop through. Any help would be appreciated.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input double(seqn cycle inAnalysis ridexprg sdmvpsu sdmvstra wtintprp) float(id_1 id_2 id_3 id_4 id_5 id_6 id_7 id_8 id_9 id_10)
    109263 5 0 . 3 156   7891.762435 0 0 0 0 0 0 0 0 0 0
    109264 5 0 . 1 155  11689.747264 0 0 0 0 0 0 0 0 0 0
    109265 5 0 . 1 157  16273.825939 0 0 0 0 0 0 0 0 0 0
    109266 5 1 2 2 168   7825.646112 0 0 0 0 0 0 0 0 0 0
    109267 5 1 3 1 156  26379.991724 0 0 0 0 0 0 0 0 0 0
    109268 5 0 . 1 155  19639.221008 0 0 0 0 0 0 0 0 0 0
    109269 5 0 . 2 152   5906.250521 0 0 0 0 0 0 0 0 0 0
    109270 5 0 . 1 150   4613.057696 0 0 0 0 0 0 0 0 0 0
    109271 5 0 . 1 167   8481.589837 0 0 0 0 0 0 0 0 0 0
    109272 5 0 . 1 155   7037.380216 0 0 0 0 0 0 0 0 0 0
    109273 5 0 . 1 155  20171.847767 0 0 0 0 0 0 0 0 0 0
    109274 5 0 . 2 167   7227.993241 0 0 0 0 0 0 0 0 0 0
    109275 5 0 . 2 164   16372.53397 0 0 0 0 0 0 0 0 0 0
    109276 5 0 . 1 171   1652.540459 0 0 0 0 0 0 0 0 0 0
    109277 5 0 . 2 161  16385.743307 0 0 0 0 0 0 0 0 0 0
    109278 5 0 . 1 171  10119.759284 0 0 0 0 0 0 0 0 0 0
    109279 5 0 . 2 169  11041.633812 0 0 0 0 0 0 0 0 0 0
    109280 5 0 . 1 155  14332.246278 0 0 0 0 0 0 0 0 0 0
    109281 5 0 . 2 150   2116.528118 0 0 0 0 0 0 0 0 0 0
    109282 5 0 . 2 164  28363.015286 0 0 0 0 0 0 0 0 0 0
    109283 5 0 . 2 149  42539.680445 0 0 0 0 0 0 0 0 0 0
    109284 5 1 2 1 154  13822.803724 0 0 0 0 0 0 0 0 0 0
    109285 5 0 . 1 163  27778.645672 0 0 0 0 0 0 0 0 0 0
    109286 5 1 1 2 154  13776.534942 0 0 0 0 0 0 0 0 0 0
    109287 5 0 . 1 150   7296.432792 0 0 0 0 0 0 0 0 0 0
    109288 5 0 . 2 151   6125.306254 0 0 0 0 0 0 0 0 0 0
    109289 5 0 . 1 171   1828.133543 0 0 0 0 0 0 0 0 0 0
    109290 5 0 . 1 171   3967.693106 0 0 0 0 0 0 0 0 0 0
    109291 5 1 2 1 161  15845.543149 0 0 0 0 0 0 0 0 0 0
    109292 5 0 . 2 150  15941.778001 0 0 0 0 0 0 0 0 0 0
    109293 5 0 . 2 163   12006.07013 0 0 0 0 0 0 0 0 0 0
    109294 5 0 . 2 167   2617.289014 0 0 0 0 0 0 0 0 0 0
    109295 5 0 . 2 155   9559.577848 0 0 0 0 0 0 0 0 0 0
    109296 5 0 . 2 156   2515.973672 0 0 0 0 0 0 0 0 0 0
    109297 5 1 2 2 154  13785.399152 0 0 0 0 0 0 0 0 0 0
    109298 5 0 . 1 167   7587.880067 0 0 0 0 0 0 0 0 0 0
    109299 5 0 . 2 155   5555.401413 0 0 0 0 0 0 0 0 0 0
    109300 5 0 . 1 169   20550.01905 0 0 0 0 0 0 0 0 0 0
    109301 5 0 . 1 170   4309.485311 0 0 0 0 0 0 0 0 0 0
    109302 5 0 . 1 152   1824.224927 0 0 0 0 0 0 0 0 0 0
    109303 5 0 . 2 152    3740.39691 0 0 0 0 0 0 0 0 0 0
    109304 5 0 . 1 149  10460.174386 0 0 0 0 0 0 0 0 0 0
    109305 5 0 . 3 156   6649.793457 0 0 0 0 0 0 0 0 0 0
    109306 5 0 . 2 172  23221.158721 0 0 0 0 0 0 0 0 0 0
    109307 5 0 . 1 160    15248.7439 0 0 0 0 0 0 0 0 0 0
    109308 5 0 . 1 152    5100.21183 0 0 0 0 0 0 0 0 0 0
    109309 5 0 . 1 169  24011.453262 0 0 0 0 0 0 0 0 0 0
    109310 5 0 . 1 159  19014.401427 0 0 0 0 0 0 0 0 0 0
    109311 5 1 3 1 166  28442.976987 0 0 0 0 0 0 0 0 0 0
    109312 5 0 . 1 163  125710.80896 0 0 0 0 0 0 0 0 0 0
    109313 5 0 . 1 156  89118.344422 0 0 0 0 0 0 0 0 0 0
    109314 5 0 . 1 165   4700.640868 0 0 0 0 0 0 0 0 0 0
    109315 5 1 2 2 170   7299.490539 0 0 0 0 0 0 0 0 0 0
    109316 5 0 . 1 150   6119.732139 0 0 0 0 0 0 0 0 0 0
    109317 5 1 2 2 154  13686.835274 0 0 0 0 0 0 0 0 0 0
    109318 5 0 . 2 151   3046.808358 0 0 0 0 0 0 0 0 0 0
    109319 5 0 . 2 169  10545.760881 0 0 0 0 0 0 0 0 0 0
    109320 5 0 . 2 169   6298.858202 0 0 0 0 0 0 0 0 0 0
    109321 5 0 . 2 168   3886.245638 0 0 0 0 0 0 0 0 0 0
    109322 5 0 . 2 161  14839.485563 0 0 0 0 0 0 0 0 0 0
    109323 5 0 . 2 154  19165.230697 0 0 0 0 0 0 0 0 0 0
    109324 5 0 . 2 151  10163.581895 0 0 0 0 0 0 0 0 0 1
    109325 5 0 . 1 169   8808.441186 0 0 0 0 0 0 0 0 0 0
    109326 5 1 2 2 152  83913.333785 0 0 0 0 0 0 0 0 0 0
    109327 5 0 . 2 154  20793.089536 0 0 0 0 0 0 0 0 0 0
    109328 5 0 . 1 168   2445.817726 0 0 0 0 0 0 0 0 0 0
    109329 5 0 . 1 168   3032.451177 0 0 0 0 0 0 0 0 0 0
    109330 5 0 . 1 160  19197.136633 0 0 0 0 0 0 0 0 0 0
    109331 5 0 . 2 160  23659.467739 0 0 0 0 0 0 0 0 0 0
    109332 5 0 . 2 166  12644.776576 0 0 0 0 0 0 0 0 0 0
    109333 5 1 2 2 166  15305.767602 0 0 0 0 0 0 0 0 0 0
    109334 5 0 . 1 157   21464.41593 0 0 0 0 0 0 0 0 0 0
    109335 5 0 . 2 163   9674.274532 0 0 0 0 0 0 0 0 0 0
    109336 5 0 . 2 155  28042.397159 0 0 0 0 0 0 0 0 0 0
    109337 5 0 . 2 170   7670.954552 0 0 0 0 0 0 0 0 0 0
    109338 5 0 . 2 161   5237.435501 0 0 0 0 0 0 0 0 0 0
    109339 5 0 . 2 159   9964.214591 0 0 0 0 0 0 0 0 0 0
    109340 5 0 . 2 172  16692.014809 0 0 0 0 0 0 0 0 0 0
    109341 5 0 . 2 155  21914.044091 0 0 0 0 0 0 0 0 0 0
    109342 5 1 2 1 155  63669.639041 0 0 0 0 0 0 0 0 0 0
    109343 5 0 . 1 153 106214.033686 0 0 0 0 0 0 0 0 0 0
    109344 5 0 . 1 158  11078.605076 0 0 0 0 0 0 0 0 0 0
    109345 5 0 . 1 151   3556.037944 0 0 0 0 0 0 0 0 0 0
    109346 5 1 2 2 171  11577.566926 0 0 0 0 0 0 0 0 0 0
    109347 5 0 . 1 162  13140.652614 0 0 0 0 0 0 0 0 0 0
    109348 5 0 . 2 156  34811.827416 0 0 0 0 0 0 0 0 0 0
    109349 5 0 . 1 154     32903.563 0 0 0 0 0 0 0 0 0 0
    109350 5 0 . 2 166  13693.376979 0 0 0 0 0 0 0 0 0 0
    109351 5 0 . 1 151  12686.956915 0 0 0 0 0 0 0 0 0 0
    109352 5 0 . 1 166   7869.179526 0 0 0 0 0 0 0 0 0 0
    109353 5 0 . 1 151  16173.232009 0 0 0 0 0 0 0 0 0 0
    109354 5 0 . 2 161   7852.874891 0 0 0 0 0 0 0 0 0 0
    109355 5 0 . 2 150   3072.661611 0 0 0 0 0 0 0 0 0 0
    109356 5 0 . 2 168   9758.906335 0 0 0 0 0 0 0 0 0 0
    109357 5 0 . 2 156   4168.110867 0 0 0 0 0 0 0 0 0 0
    109358 5 0 . 2 157 132131.703358 0 0 0 0 0 0 0 0 0 0
    109359 5 0 . 2 151  13141.893774 0 0 0 0 0 0 0 0 0 0
    109360 5 0 . 1 170  22688.293963 0 0 0 0 0 0 0 0 0 0
    109361 5 0 . 1 158   8629.285118 0 0 0 0 0 0 0 0 0 0
    109362 5 0 . 1 168   6007.404244 0 0 0 0 0 0 0 0 0 0
    end
    Code:
    cd "path"
    foreach v of varlist id_1-id_10 {
        quietly sum `v'
        quietly gen value_`v'=r(mean)
        if value_`v'!=0 {
            estpost svy, subpop(if inAnalysis==1 & ridexprg!=.): tab `v', col percent ci
            estout using `v'_results.txt, cell("b(label(percent)) lb ub") replace
        }
        drop value_`v'
        }
    
    * part2     
    clear all
    
    foreach v of varlist id_1-id_10 {
        import delimited "path\\`v'_results.txt", varnames(2) clear
        keep if v1=="1"
        gen med_name="`v'"
        drop v1
        save `v'_results.dta, replace
        }
        
    
    * part 3
    clear all
    
    foreach drugs in `v'{
        append using `v'_results.dta
    }

  • #2
    After clear all there is no data in memory and you have no variables to refer to. So, I think your loop needs to be different.


    Code:
    clear all  
    
    forval j = 1/10 {    
        import delimited "path\\id_`j'_results.txt", varnames(2) clear    
        keep if v1=="1"    
        gen med_name="id_`j'"    
        drop v1    
        save id_`j'_results.dta, replace
    }
    The first block can be slimmed down as you don't need to put a mean in a variable only to lose it immediately.

    Code:
    foreach v of varlist id_1-id_10 {    
         sum `v', meanonly          
         if r(mean) !=0 {        
            estpost svy, subpop(if inAnalysis==1 & ridexprg!=.): tab `v', col percent ci        
            estout using `v'_results.txt, cell("b(label(percent)) lb ub") replace    
         }  
    }
    However, I understand this to mean that a file won't be posted if the mean of any variable is zero.

    If so, the second block needs to be a loop over files that exist.
    Last edited by Nick Cox; 22 Nov 2022, 15:14.

    Comment


    • #3
      Nick Cox , I realized after submitting the query that I left out the path to reference the files that exist. I appreciate the consolidation of the code--I managed to resolve the error and run my code successfully.

      Comment

      Working...
      X