Announcement

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

  • Survey Dataset failing foreach loop "no observation in subpop() subpopulation" r(461)

    Hello all

    I am using a multiyear survey dataset and tabout user written command (written by Ian watson) to export results.

    I am frequently running into the problem where the loop fails and says "no observation in subpop() subpopulation" r(461). I have attached the screenshot for this thread.

    As the loop was failing multiple times, I tried to use the if else command as follows

    count if (CASE_SURGERY_ANY_DX ==1 & CASE_`h'_PRI ==1 & YEAR==`l' )

    if `r(N)' >0 {

    tabout command

    }


    else {

    di "no subpopulation"

    }

    However the loop is still failing and I am not sure how to rectify it.
    I was hoping to see if any of the stata experts can help me.

    I have posted the code and screenshot as attachment

    Thank you,
    Praveen


    Code:
    
    capture log close
    log using "surgery.log", replace
    foreach h in  MALIG BEN   {
    
    qui use YEAR KEY  CASE_PRO_MULTI_PRI     ///
        CASE_`h'_PRI  CASE_SURGERY_ANY_DX  using "Z:/surgery_2007.dta", clear
        
        
    forvalues i=2008/2011{
    append   using "Z:/surgery_`i'.dta" , keep(YEAR KEY CASE_PRO_MULTI_PRI CASE_`h'_PRI CASE_SURGERY_ANY_DX)
    
                         }
    
    forvalues i=2007/2011{
            
            merge 1:1 KEY using "Z:/NIS_CORE_DESIGN_`i'.dta",keepusing(KEY  DISCWT HOSPID NIS_STRATUM YEAR) update    
            drop _merge
            
            
            merge 1:1 KEY using "Z:/NIS_CORE_ANALYSIS1_`i'.dta", keepusing(KEY  FEMALE PAY1 RACE  ELIXHAUSER_CAT) update
            drop _merge
    
                      }
        
    *   SVYSET THE POPULATON
            
    svyset [pweight= DISCWT ],strata  (NIS_STRATUM ) psu( HOSPID )
    
            
            * setup macros for loops
                
                levelsof YEAR , local(levels)
                local YEARlabels : value label YEAR
                local counter = 0
                local filemethod = "replace"
                local heading = ""
                
                
                * begin looping through the values of the by category
                               foreach l of local levels {
                              
                               count if (CASE_SURGERY_ANY_DX ==1 & CASE_`h'_PRI  ==1 & YEAR==`l' )
    
                                        if  `r(N)' >0 {
                                        
                                                    if `counter' > 0 {
                                                       local filemethod = "append"
                                                       local heading = "h1(nil) h2(nil)"
                                                                     }
                                                    local vlabel : label `YEARlabels' `l'
    
                                                 tabout  FEMALE RACE PAY1 ELIXHAUSER_CAT  CASE_PRO_MULTI_PRI if YEAR == `l' & (CASE_SURGERY_ANY_DX ==1 & CASE_`h'_PRI  ==1 ) using  ///
                                                         "C:/data/`h'_count_3.xls", `filemethod' `heading' h3("YEAR: `vlabel'")   c(freq ) f(0c 1 ) svy  mi
                      
                
                                        local counter = `counter' + 1    
                                    
                                        
                                                      }
                                        
                                        
                                          else      {
                                             di "no subpopulation"
                                                      }
    
      }
      }
            *
    Attached Files
    Last edited by Praveen Harish; 21 Jun 2014, 22:28.

  • #2
    I've never used tabout, but it looks like what it is doing is calling a command with the svy, subpop() prefix repeatedly to build the table based on the varlist you specified. And in (at least) one case, the corresponding subpopulation is empty (i.e., the error message you see comes from Stata, not from tabout). I don't know if tabout has any facility for handling this condition (as I said, I've never used it), but if it doesn't, then you'll have to do this another way. My suggestion would be to construct a table of counts manually using the variables you passed to tabout, and you should see that at least one cell has a count of zero.

    Comment


    • #3
      Thank you for the tip.
      I got it.
      Thanks again
      Praveen

      Comment

      Working...
      X