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


From   Eric Booth <[email protected]>
To   "<[email protected]>" <[email protected]>
Subject   Re: AW: st: Create variable as a copy of a dynamically calculated second variable
Date   Thu, 9 Sep 2010 18:09:05 +0000

<>

Sure -- sometimes its helpful if you present the data you've got and what you'd like it to look like after doing what you've described.

It sounds like there are two processes going on here, and I don't think there's a good solution that will help you create "newvariable" out of both variables with "year" suffixes (e.g., 2001) and variables with "alpha" suffixes (e.g., "DEF") in the same loop.   Besides, you've mentioned that you want to increase the year by one when dealing with your "yearly" values and skip adding anything when it is a "value" variable with an "alpha" suffix.  

So, thinking of these two types of variables separately, here's an example:

********
clear
**Watch for wrapping issues**
inp str5(first) valuesABC valuesDEF valuesGHI valuesJKL firstyear value2000 value2001 value2002 
ABC 2 3 4 5 1 1 2 1
DEF 1 1 1 1 2 2 3 1
GHI 8 8 8 8 3 4 3 1
JKL 9 9 9 9 8 1 8 1
end

//1. Yearly Values: Obtain Next Year's Value//
gen newvariable_year = .
forval x = 2000/2002 {
 cap replace newvariable_year = value`=`x'+1' if firstyear == value`x'
}
l firstyear-newvariable_year


//2. Alpha Values: Obtain Same Year's Value//
g newvariable_alpha = .
ds values*
local vars `r(varlist)'
di `"`vars'"'
local novalues:subinstr local vars "values" "", all
di `"`novalues'"' //obtain just the alpha suffixes
foreach x in `novalues' {
replace newvariable_alpha = values`x' if first=="`x'"
}
l first values* newvariable_alpha
*********

~ Eric
__
Eric A. Booth
Public Policy Research Institute
Texas A&M University
[email protected]


On Sep 9, 2010, at 8:35 AM, Wiemann, Markus wrote:

> 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: [email protected] [mailto:[email protected]] Im Auftrag von Eric Booth
> Gesendet: Donnerstag, 9. September 2010 14:49
> An: <[email protected]>
> 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
> [email protected]
> 
> 
> 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
>> 
>> 
>> 
>> *
>> *   F

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