Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: How to run a loop on labels

From   Nick Cox <>
To   "" <>
Subject   Re: st: How to run a loop on labels
Date   Wed, 3 Jul 2013 15:55:07 +0100

Stata is very good at this kind of thing.

First, -ds- has all kinds of handles for finding lists of variables.

ds, has(varlabel *1)

identifies all variables whose variable label ends in 1 and the list
of their names is left behind in r(varlist).

So you can go

foreach v in `r(varlist)' {
         local label : var label `v'
         local label : subinstr local label "1" "Administrative Office"
         label var `v' "`label'"

Naturally, watch out: if the character "1" occurs for other reasons,
you need to do it differently, but I have high hopes that this will

Incidentally, if you like -ds-, then -filename- (SJ) is yet more
versatile and has better syntax in my view. (In saying that I am
criticising my own syntax incorporated in the official -ds-.)

-findname- can do things like this

Identify all variables that are constant

findname, all(@ == @[1])

Identify all variables that are indicators or have system missing as a value

findname, all(inlist(@, 0, 1, .))

Identify all variables with names longer than 20 characters

findname, all(length("@") > 20)


On 3 July 2013 15:23, Alice Guerra <> wrote:

> I have a list of variables' names and corresponding labels:
> x1 label: Total duration in 1
> y1 label: Total people in 1
> z1 label: Total costs in 1
> x2 label: Total duration in 2
> y2 label: Total people in 2
> z2 label: Total costs in 2
> 1 corresponds to Administrative Office, and 2 to Economic Department.
> How can I substitute the 1 with Administrative Office in all the
> labels of variables that end with 1? And, that is the same, how can I
> substitute the 2 with Economic Department in all the labels of
> variables that end with 2?
> Is it possible to run a loop for labels?
*   For searches and help try:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index