Announcement

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

  • Hiding empty table from output

    Hello again

    On this project, I have some variables and two of them are location. The first one identifies the location (1,2,3) and when selected 3, the other variable (location_extra) should be some string. The point is, a lot of locations are 1 and 2 and only a few are 3. I want to list all missing variables. I could do
    Code:
    list if missing()
    .
    When I do this, my output is spammed with rows containing only the location_extra missing. So I did:
    Code:
    foreach var of varlist id-lugar nccdtreat-recusa01{ //that excluded my location_extra variable only
                                by usf interid: list if missing(`var')
        }
    (usf and interid and identifiers variables I have) (I know, i could have dropped location_extra just to list them)

    Then my output is as follows:
    Code:
    ...
    -> usf = 06, interid = 33
    
    ------------------------------------------------------------------------------------------------
    -> usf = 06, interid = 41
    
    ------------------------------------------------------------------------------------------------
    -> usf = 06, interid = 42
    
    ------------------------------------------------------------------------------------------------
    -> usf = 06, interid = 91
    
    ------------------------------------------------------------------------------------------------
    -> usf = 07, interid = 17
    
    ...
    That log came with like 1000 lines and only 32 tables on them.
    Is there a way to not print those empty stuff, getting only the tables so my log doesn't have a lot of unuseful info?

    George

  • #2
    I'm not sure if this is quite what you want, but try:

    Code:
    ds location_extra, not
    local wanted `r(varlist)'
    local wanted: subinstr local wanted " " ", ", all
    sort usf interid
    list if missing(`wanted'), sepby(usf interid)
    This will give you lists of observations containing missing values for any variable other than location_extra, batched by usf and interid. But there will be no headers or wasted space.

    Comment


    • #3
      Originally posted by Clyde Schechter View Post
      I'm not sure if this is quite what you want, but try:

      Code:
      ds location_extra, not
      local wanted `r(varlist)'
      local wanted: subinstr local wanted " " ", ", all
      sort usf interid
      list if missing(`wanted'), sepby(usf interid)
      This will give you lists of observations containing missing values for any variable other than location_extra, batched by usf and interid. But there will be no headers or wasted space.

      sorry for my delay.
      That come close to what I want. It doesn't separate by usf and interid. Lemme show you what I want by pictures, I think it will be clearer.
      Last edited by George Scotton; 21 Jun 2016, 08:59.

      Comment


      • #4
        Here are the differences. I want what you did but with my header on "usb interid". Is it possible?.
        edit: First pic is what I want, second is yours code and last is what I have.
        Attached Files

        Comment


        • #5
          I do not understand your data setup so the following guess may not help at all; I suggest preserving your data, dropping all situations that would result in an empty table, getting your tables and the restoring your data; see "h preserve";

          if this turns out to not be helpful, please read the FAQ, install dataex and show us what your data look like (at least a sample of it; if confidential, make up some data with a similar structure)

          Comment


          • #6
            OK, I see the problem. The output of my code actually does segregate the listed observations by usf and interid, but it doesn't print separating headers. Also, the -sepby()- option works best when each observation only produces a single line of output. In that situation, Stata draws lines between the groups of observations. So I think that would have been workable with fewer variables. But it doesn't work well for what you have. Try this instead:

            Code:
            ds location_extra, not
            local wanted `r(varlist)'
            local wanted: subinstr local wanted " " ", ", all
            
            levelsof usf, local(usfs)
            levelsof interid, local(interids)
            
            foreach u of local usfs {
                foreach i of local interids {
                    quietly count if missing(`wanted') & usf == `"`u'"' & interid == `i'
                    if r(N) > 0 {
                        display as result `"usf  == `"`u'"', interid == `i'"'
                        list if missing(`wanted') & usf == `"`u'"' & interid == `i'
                    }
                }
            }
            Note: the above code assumes usf is a string variable and interid is numeric. I am confident of the former based on your output; for the latter I cannot tell. If interid is also a string variable then `i' needs to be wrapped in compound double quotes the way `u' is.

            A shorter, simpler way, if you are willing to drop the observations that will not be listed from the data actively in memory:

            Code:
            ds location_extra, not
            local wanted `r(varlist)'
            local wanted: subinstr local wanted " " ", ", all
            
            levelsof usf, local(usfs)
            levelsof interid, local(interids)
            
            keep if missing(`wanted')
            by usf interid, sort: list

            Comment


            • #7
              Originally posted by Clyde Schechter View Post
              OK, I see the problem. The output of my code actually does segregate the listed observations by usf and interid, but it doesn't print separating headers. Also, the -sepby()- option works best when each observation only produces a single line of output. In that situation, Stata draws lines between the groups of observations. So I think that would have been workable with fewer variables. But it doesn't work well for what you have. Try this instead:

              Code:
              ds location_extra, not
              local wanted `r(varlist)'
              local wanted: subinstr local wanted " " ", ", all
              
              levelsof usf, local(usfs)
              levelsof interid, local(interids)
              
              foreach u of local usfs {
              foreach i of local interids {
              quietly count if missing(`wanted') & usf == `"`u'"' & interid == `i'
              if r(N) > 0 {
              display as result `"usf == `"`u'"', interid == `i'"'
              list if missing(`wanted') & usf == `"`u'"' & interid == `i'
              }
              }
              }
              Note: the above code assumes usf is a string variable and interid is numeric. I am confident of the former based on your output; for the latter I cannot tell. If interid is also a string variable then `i' needs to be wrapped in compound double quotes the way `u' is.

              A shorter, simpler way, if you are willing to drop the observations that will not be listed from the data actively in memory:

              Code:
              ds location_extra, not
              local wanted `r(varlist)'
              local wanted: subinstr local wanted " " ", ", all
              
              levelsof usf, local(usfs)
              levelsof interid, local(interids)
              
              keep if missing(`wanted')
              by usf interid, sort: list
              that workd exactly as expected =). Thank you very much sir. I will save this code for further use on other datasets and variables aswell.
              And I don't have a problem on dropping the obs. We can always just not save the dataset after dropping and reopening it.
              edit: just to say that the second code works better. The long one won't have separators between tables. The second does.

              Thanks again!
              Click image for larger version

Name:	Untitled.png
Views:	1
Size:	17.9 KB
ID:	1346232

              Comment


              • #8
                Now just some dumb question: what exactly
                Code:
                 
                 local wanted: subinstr local wanted " " ", ", all
                does?

                Comment

                Working...
                X