Announcement

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

  • capture and postfile

    HI all,

    I am experiencing some odd behavior in postfile, Basically i am trying to create a list of value labels in a very large data set. uselabel doesn't appear to work. Therefore i am resorting to more manual methods.

    This all works except when i encounter some dodgy labels. Therefore i intended to capture the post statement, write an alternative post statement, and let the loop continue on its merry way.

    However, when the post statment errors it appears to close the open postfile, and then next post statement errors out. I thought postfile were supposed to remain open until they are closed.

    Any suggestions of how to fix this.

    Code:
    cap postclose labellist
    postfile labellist str100 variable str100 value_label level str200 level_txt ///
        using labellist.dta, replace
    
    foreach v of varlist  *     {
    local vallab : value label `v'
    if  "`vallab'" !="" {
    labelsof  `v'
    
        foreach  i in `r(values)' {
            local value :label (`v') `i'
    
                capture  post labellist (`"`v'"') (`"`vallab'"') (`i') (`"`value'"')
    
                 if _rc!=0 {
                  post labellist (`"`v'"') (`"`vallab'"') (`i') (`"Bad label"')
                 }
    
                    } // End i loop
    
                    } // end if
                    } // end v loop
    
    cap postclose labellist

  • #2
    This recreates the error
    Code:
    sysuse auto.dta, clear
    label define origin1 4 `"t```est"' , add modify
    label values foreign origin1
    
    set trace on
    cap postclose labellist
    postfile labellist str100 variable str100 value_label level str200 level_txt ///
        using labellist_.dta, replace
    
    foreach v of varlist  *     {
    local vallab : value label `v'
    if  "`vallab'" !="" {
    labellist  `v'
    
        foreach  i in `r(values)' {
            local value :label (`v') `i'
    
                capture  post labellist (`"`v'"') (`"`vallab'"') (`i') (`"`value'"')
    
                 if _rc!=0 {
                  post labellist (`"`v'"') (`"`vallab'"') (`i') (`"Bad label"')
                 }
    
                    } // End i loop
    
                    } // end if
                    } // end v loop
    
    cap postclose labellist

    Comment


    • #3
      My mind starts hurting the moment I see `"` and its many variations, so I can't help you with the post issue (and I think for many the barrier to understand what you're doing exactly is too large too). But perhaps we can get somewhere by taking a step back.

      What exactly do you have and where do you want to end? The way I understand it you have a variable in a very large set that has value labels? You want to create a list of these value labels? To what purpose? What do you mean when you say "uselabel doesn't appear to work"?

      Have you tried label save <labelname> using <dofilename>? Or am I misunderstanding your intentions?

      Comment


      • #4
        Hi Jesse,

        Im simply trying to create data dictionary of a dataset. -describe, replace clear- did the trick for the variables and attributes. But when it comes to value labels life is more complicated.

        label save almost works, but it puts the results in a dofile and has a few random carriage returns where it shouldn't.

        However the problem i am generally referring to is that of when a post fails, it seems to close the open postfile, and therefore you can't capture the error, post something else, and let post resume on its next occasion.





        Comment


        • #5
          The soloution to my problem appears to test the post statement first in sacrificial postfile, then if its clean, let it post to the real one.

          Its ugly, but it works, but i didnt really think the postfile command was intended to close unless your instructed it .

          Code:
          cap postclose labellist
          postfile labellist str100 variable str100 value_label level str200 level_txt ///
              using labellist.dta, replace
          
          foreach v of varlist  *     {
          local vallab : value label `v'
          if  "`vallab'" !="" {
          labelsof  `v'
          
              foreach  i in `r(values)' {
                  local value :label (`v') `i'
          
          cap postclose test
          postfile test str200 level_text using I:\junk.dta, replace
          cap post test (`"`value'"')
          
          if _rc==0 {        
                post labellist (`"`v'"') (`"`vallab'"') (`i') (`"`value'"')
                      }
          else {            
                post labellist (`"`v'"') (`"`vallab'"') (`i') (`"Bad label"')
                       }
          capture postclose test
          
                          } // End i loop
          
                          } // end if
                          } // end v loop
          
          cap postclose labellist

          Comment

          Working...
          X