Announcement

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

  • Standardizing labels before appending

    I am trying to use the classic -decode- / -encode- combination that one should use when appending datasets with a variable labeled differently across files. However, it appears that in the different files I am trying to append, one variable can :

    1) either have a label with labeled values
    2) or have a label but without labeled values

    I am using the following code:

    Code:
    foreach country of global countrieslist {
        use ``country'', clear
        foreach varlabel of global label_variables {
            capture confirm variable `varlabel' // Check whether variable exists in every dataset
            if !_rc {
                    decode `varlabel', gen(_`varlabel')
                    drop `varlabel'
                    rename _`varlabel' `varlabel'
            }
        }
        save ``country'_AR', replace
    }
    
    foreach country of global countrieslist {
        append using ``country'_AR'
    }
    But for datasets having variables facing the problem 2), I get the following output:

    value label A4B not found
    r(111);

    Indeed, when I run
    Code:
    codebook a4b
    , it is written "Type: Numeric (byte) Label: A4B, but label does not exist".

    What can I do to make this code run? The loop stopped at this dataset:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte(a1 a4b) str19 a4a
    12 52 "Retail"            
    12 52 "Retail"            
    12 51 "Other Services"    
    12 51 "Other Services"    
    12 52 "Retail"            
    12 52 "Retail"            
    12 51 "Other Manufacturing"
    12 51 "Other Manufacturing"
    12 52 "Retail"            
    12 52 "Retail"            
    12 51 "Other Services"    
    12 51 "Other Services"    
    12 52 "Retail"            
    12 52 "Retail"            
    12 52 "Other Services"    
    12 52 "Other Services"    
    12 51 "Other Services"    
    12 51 "Other Services"    
    12 52 "Retail"            
    12 52 "Retail"            
    end
    label values a1 A1
    label def A1 12 "El Salvador", modify
    label values a4b A4B
    a4a is a variable that got successfully decoded, a4b did not. I could try using the command "capture label val `varlabel'" within the loop, but I think this will make it worse as I think it will make my labeled variables label-less. Is there some sort of way to assert that a variable actually has labels for its values ?

    Thanks for the help!
    Last edited by Hugo Denis; 09 Sep 2022, 04:14.

  • #2
    Take a look at this code. I first generate data files which have differently constructed variable foreign, then create a loop to deal with that issue.

    Code:
    * creating three example files
    
    sysuse auto, clear
    unab variables: *
    tempfile country1 // this one has the -foreign- variable numeric and with a value label
    save `country1'
    
    sysuse auto, clear
    label drop origin // this replicates the scenario where a variable has a value label attached, but the label does not exist
    noi codebook foreign
    tempfile country2
    save `country2'
    
    sysuse auto, clear
    decode foreign, gen(_foreign)
    drop foreign
    rename _foreign foreign
    tempfile country3
    save `country3' // this has the variable already in string form
    
    global countries country1 country2 country3
    
    clear
    save appended, replace emptyok
    
    foreach country of global countries {
        use ``country'', clear
        foreach appendvar of local variables {
            capture confirm numeric variable `appendvar'
            if _rc != 0 continue
    
            local has_good_lab 0                
            local val_lab: value label `appendvar'
            if "`val_lab'" != "" {
                capture label li `val_lab'
                if _rc == 0 local has_good_lab 1
            }
    
            if `has_good_lab' {
                decode `appendvar', gen(_`appendvar')
                drop `appendvar'
                rename _`appendvar' `appendvar'
            }
            else tostring `appendvar', replace force
        }
        append using appended
        save appended, replace
    }

    Comment

    Working...
    X