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: Create variable as a copy of a dynamically calculated second variable

 From Eric Booth To "" Subject Re: st: Create variable as a copy of a dynamically calculated second variable Date Thu, 9 Sep 2010 12:49:11 +0000

```<>

It isn't clear what you want.  You mention "read"ing the value out of variable "Value2006" if "firstyear"=="Value2005", but your code to generate "newvariable" doesn't include anything about "value2006" .
Furthermore,  "Value[String(FirstYear + 1)] "    doesnt make any sense.    "Value()" is not a function.
This is likely pseudo-code meaning something like  "Value2006 * (FirstYear +1)" and you want to run it if "firstyear==value2005" (?)  ((Not sure why you included string() either.))
If my interpretation of your pseudo-code is right, here's a way to do that:

****************!
clear
inp first value2000 value2001 value2002 value2003 value2004 value2005 value2006
1 2 3 4 5 6 7 8
1 1 1 1 1 1 1 1
9 9 9 9 9 9 9 9
8 8 8 8 8 8 8 8
end

g myvalue = value2006 if first==value2005
g newvariable = myvalue*(first + 1)
li first value2005-newvariable

//or you can reshape it long and use subscripting//
g i = _n
rename first value1  //note that I've renamed "first"
drop myvalue newvariable
reshape long value, i(i) j(year)
bys i:  g newvariable = value * (value[1]+1) if year==2006 & value[1]==value[5]
li year value newvariable if !mi(newvariable)
****************!

Eric
__
Eric A. Booth
Public Policy Research Institute
Texas A&M University
ebooth@ppri.tamu.edu

On Sep 9, 2010, at 4:32 AM, Wiemann, Markus wrote:

> Hi everyone,
>
> I am not sure whether the following is possible in Stata and, if yes,
> how to address the problem.
>
> I want to assign a new variable a value that is already stored in a
> second variable. The problem is that I need a value of a variable that
> is calculated dynamically. Let me explain it in a short example: Assume
> I have the following variables:
>
> FirstYear
> Value2000
> Value2001
> Value2002
> ...
> Value2010
>
>
> What I need is something like
> . gen NewVariable = Value[String(FirstYear + 1)]
> that would read the value out of Value2006 if FirstYear had the value
> 2005. Is this possible in Stata? Would I need to write a macro for that?
>
>
>
> Thanks and best regards
> Markus
>
>
>
> *
> *   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/

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