Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | "Wiemann, Markus" <m.wiemann@fs.de> |
To | <statalist@hsphsun2.harvard.edu> |
Subject | AW: st: Create variable as a copy of a dynamically calculated second variable |
Date | Thu, 9 Sep 2010 15:35:45 +0200 |
Eric, Thank you for your quick reply. I guess you are right and I did not make my intention really clear. My code example was pseudo-code which was not intended to work like that in Stata. What I want - expressed in Stata code - is the following: . gen newvariable = . . replace newvariable = value2001 if firstyear == 2000 . replace newvariable = value2002 if firstyear == 2001 Etc I know this could be done in a loop, but in the future I might also need it with alphabetic variables like in the following (of course, in this case, without adding 1 to the value in firstyear): ******** clear inp str3 first valueABC valueDEF valueGHI valueJKL ABC 2 3 4 5 DEF 1 1 1 1 GHI 8 8 8 8 JKL 9 9 9 9 end gen newvariable = . replace newvariable = valueABC if strmatch(first, "ABC") replace newvariable = valueDEF if strmatch(first, "DEF") replace newvariable = valueGHI if strmatch(first, "GHI") replace newvariable = valueJKL if strmatch(first, "JKL") ********* So, in my case, it would be easiest to somehow tell Stata that I want it to copy the value which is stored in the variable named "valueXYZ" with XYZ whatever the value of firstyear is. Do you know what I mean? Is this possible like that? Markus -----Ursprüngliche Nachricht----- Von: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] Im Auftrag von Eric Booth Gesendet: Donnerstag, 9. September 2010 14:49 An: <statalist@hsphsun2.harvard.edu> Betreff: Re: st: Create variable as a copy of a dynamically calculated second variable <> 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/ * * 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/