I'd like to create a variable with a value from a certain other variable, where the other variable the value comes from depends on the value of yet another variable. An example might help: the observations are journal articles, and I have the year they were published, as well as the number of citations each article got in each calendar year. I want a new variable that is the number of citations an article received 5 years after publication.
I did this with a loop, but Is there a nicer, loop-free way to write this code? Here's a working example:
*Make the original data set
clear all
set seed 1492
set obs 100
*Values for each year (ex: citation count for an article in a given year)
forvalues X=2000/2015 {
gen _`X'value=round(runiform(0,10),1)
}
*Assign the year (ex: publication year of an article)
gen year=2000+floor(runiform(0,8))
*Create new var with value from other variable, which var depends on value of 'year'
*Create var with value from 5 years after publication
gen year5value=.
forvalues X=2000/2007 {
replace year5value=_`=`X'+5'value if year==`X'
}
I did this with a loop, but Is there a nicer, loop-free way to write this code? Here's a working example:
*Make the original data set
clear all
set seed 1492
set obs 100
*Values for each year (ex: citation count for an article in a given year)
forvalues X=2000/2015 {
gen _`X'value=round(runiform(0,10),1)
}
*Assign the year (ex: publication year of an article)
gen year=2000+floor(runiform(0,8))
*Create new var with value from other variable, which var depends on value of 'year'
*Create var with value from 5 years after publication
gen year5value=.
forvalues X=2000/2007 {
replace year5value=_`=`X'+5'value if year==`X'
}
Comment