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


From   Maarten Buis <[email protected]>
To   [email protected]
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
and conceal your identity. "

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


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