Programs problem set question
Opened this issue · 3 comments
Here's a potential problem set question for a module on writing programs. I often use it when training others.
label list
lists the contents of a value label:
sysuse auto, clear
label list origin
However, label list
does not accept variable names as arguments: it requires value label names. This means that listing the contents of a variable's value label takes two steps: one to determine the label name and another to list that label. For instance, label list
results in an error if specified the name of the value-labeled foreign
variable in the auto dataset:
sysuse auto, clear
* Results in an error.
label list foreign
* We need to first determine the value label of foreign:
describe foreign
label list origin
Write a program that avoids this intermediary step. Named varval
, the program should take a varlist
, then list the value label of each variable in the list.
There are a variety of solutions. Here's one:
program varval
version 11
syntax varlist(num)
foreach var of local varlist {
local lab : val lab `var'
if "`lab'" != "" {
disp _newline "{txt}Value label for {res:`var'}:"
lab list `lab'
}
}
end
Stata Training Course 151 could provide the basis for a future module on writing programs.
See thread 'Random Help - Options for Options for an .ado" for an issue in writing programs
I just added this to the new Stata 151 milestone. Milestones are a good way to group together related issues. For example, I think your last comment is a distinct idea, so should be its own issue.