-1

I want to list a variable's name, label, as well as all values and value labels on one line, for each variable in the dataset.

For example:

foreign, Car type, 0 Domestic 1 Foreign

I managed to write some code to do this, but it only shows value labels that are used in the dataset:

sysuse auto, clear

foreach var of varlist * {
    local _check: val l `var'
    if `"`_check'"' != "" {
        quietly fre `var'
        di "`var'" char(44) `"`: var label `var' '"' char(44) as result r(lab_valid)
        continue    
        }
    else{
        di "`var'" char(44) `"`: var label `var' '"'    
        continue
        }
}

Here the community-contributed command fre would only include 0 Domestic, if the data did not include 1 Foreign.

There are easier ways to show value labels, but all show them on different lines.

Stalfos
  • 1
  • 2
  • See `help limits` for the limits on number and length and value labels, which could singly or jointly overwhelm a single line. You're evidently not thinking that that could bite _you_. – Nick Cox Mar 26 '19 at 18:02

1 Answers1

0

Using the option include in fre solves the problem of wanting to list all value labels one line, even those that aren't used in the data.

For example:

foreach var of varlist * {  
    local _check: val l `var'
    if `"`_check'"' != "" {
        quietly fre `var', include
        di "`var'" char(44) `"`: var label `var' '"' char(44) as result r(lab_valid)
        continue    
        }
    else{
        di "`var'" char(44) `"`: var label `var' '"'    
        continue
        }
}
Stalfos
  • 1
  • 2