Bookmark and Share

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


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

Re: st: converting numbers to strings


From   Nick Cox <[email protected]>
To   [email protected]
Subject   Re: st: converting numbers to strings
Date   Tue, 17 Jul 2012 14:59:09 +0100

foreach n in 07 09 11 13 {
         replace abc`n' = myfunc( abc`n')
}

would work here. Also,

forval n = 7(2)13 {
      local n : di %0.2f `n'
      replace abc`n' = myfunc( abc`n')
}

See also

Cox, N.J. 2010. Stata tip 85: Looping over nonintegers. Stata Journal
10(1): 160-163.

When I say "would work", the presumption is that "myfunc" is replaced
by a legal function name.

Nick

On Tue, Jul 17, 2012 at 2:08 PM, Maarten Buis <[email protected]> wrote:

> 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.

 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?
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


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