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, statalist.org is already up and running.

# Re: st: How to use functions within variable names

 From Maarten Buis To statalist@hsphsun2.harvard.edu Subject Re: st: How to use functions within variable names Date Wed, 17 Aug 2011 09:53:16 +0200

```On Wed, Aug 17, 2011 at 3:14 AM, Sdfsdfsadfsf Sdfsdf wrote:
> is it possible to use the result of a function or mathematical operation as a part of a variable name?
>
> Here is a simple example what I want to do
> forvalue i=1(1)2 {
> rename r`i'= r[`i'-1]    // Instead of the operation in the squared brackets there should be the result eg.0 for 1-1
> }

rename r`i' = r`=`i'-1'

See: <http://www.maartenbuis.nl/example_faq/index.html#%60=%27>

> or an more accurate example with variables
>
> forvalue i=1(1)100 {
> replace b_`i'= a[c+`i']
> }
>
> c is a variable with integer values
> The result of c+`i' determines the variable from which the value of b_`i'is taken from
>
>
> eg    `i'=3; c=7;
>         b_3 =a10

Here you need a very different strategy:

*------------- begin example --------------
clear
input str3 b1 str3 b2 c
foo     bar     8
bla     bla     1
end

forvalues i=1/2 {
replace b`i' = "a" + string(c + `i')
}
list
*-------------- end example ---------------
(For more on examples I sent to the Statalist see:
http://www.maartenbuis.nl/example_faq )

Hope this helps,
Maarten

Ps. Note that the Statalist FAQ states
<http://www.stata.com/support/faqs/res/statalist.html#tojoin>:
"It is long-standing practice on Statalist that most members,
especially the most active members who supply a large fraction of the
answers, post using their real names. This is one of the ways in which
we show respect to others. So we discourage you from posting from
behind fake names or identifiers. <snip> Please note that many members
are less inclined to answer anonymous emails, sometimes to the point
of ignoring them on principle. These choices are personal, but your
chances of eliciting a good reply are greatly diminished if you write

--------------------------
Maarten L. Buis
Institut fuer Soziologie
Universitaet Tuebingen
Wilhelmstrasse 36
72074 Tuebingen
Germany

http://www.maartenbuis.nl
--------------------------

*
*   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/
```