Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, is already up and running.

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

Re: st: converting numbers to strings

From   Maarten Buis <>
Subject   Re: st: converting numbers to strings
Date   Tue, 17 Jul 2012 15:08:44 +0200

On Tue, Jul 17, 2012 at 3:00 PM, Pradipto Banerjee wrote:
> I have four variables, say abc07, abc09, abc11, abc13. I wanted to run a loop like
> forvalues n = 7(2)13 {
>         replace abc`n' = myfunc ( abc`n')
>        }
> The code gives an error because it doesn't find abc7 or abc9 (because the columns were named abc07, abc09). Is it possible to rewrite the code to make it accept abc07 and abc09 instead of abc7 and abc9?

What about:

foreach var of varlist abc?? {
    replace `var' = myfunc(`var')

The trick is that the varlist abc?? is all variables starting with
"abc" and two other characters. This is not exactly the same as what
you asked for, as this would also include variables like abcde or
abc15 if they are present in the data. However, if such variables are
not present in your data, than this would be a solution.

Hope this helps,

Maarten L. Buis
Institut fuer Soziologie
Universitaet Tuebingen
Wilhelmstrasse 36
72074 Tuebingen
*   For searches and help try:

© Copyright 1996–2016 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index