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: How to use functions within variable names

From   Maarten Buis <>
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: <>

> 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 --------------
input str3 b1 str3 b2 c
      foo     bar     8
      bla     bla     1

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

Hope this helps,

Ps. Note that the Statalist FAQ states
"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
and conceal your identity. "

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