Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: st: Populating time series records with values from different "Year" columns (rep78, rep79...)


From   "Robak, Anna Michelle - robam013" <robam013@mymail.unisa.edu.au>
To   "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu>
Subject   RE: st: Populating time series records with values from different "Year" columns (rep78, rep79...)
Date   Wed, 21 Dec 2011 22:26:49 +0000

Yes, that's it - brilliant, thank you! 

________________________________________
From: owner-statalist@hsphsun2.harvard.edu [owner-statalist@hsphsun2.harvard.edu] on behalf of Austin Nichols [austinnichols@gmail.com]
Sent: 22 December 2011 10:53
To: statalist@hsphsun2.harvard.edu
Subject: Re: st: Populating time series records with values from different "Year" columns (rep78, rep79...)

Anna Michelle - robam013 <robam013@mymail.unisa.edu.au>:

Do you mean to do something like this?

sysuse auto, clear
gen rep79=rep78+1
expand 2
bys make: gen Year=1977+_n
levelsof Year, loc(ys)
g annrep=.
foreach y of loc ys {
 loc i=substr("`y'",3,2)
 replace annrep=rep`i' if Year==`y'
 }


On Wed, Dec 21, 2011 at 4:19 PM, Robak, Anna Michelle - robam013
<robam013@mymail.unisa.edu.au> wrote:
> Dear List,
>
> I want to populate a variable "annrep" with values from variables rep78, rep79...rep118, where the first annrep record takes the value from rep78, the second takes the value from rep79, etc. But rather than taking the value from rep78, rep79...rep118, Stata is simply entering 'rep78', 'rep79'...
>
> I expanded my records to create time series data, and generated a new variable "Year". For each Year, I want to grab the appropriate rate from columns rep78, rep79, etc, but I can't seem to get Stata to recognise my concatenated string as a variable name. This is a simplified version of the command I tried, using just rep78 and rep79:
>
> sysuse auto, clear
> gen rep79=rep78+1
> expand 2
> by make,sort: gen Year=1977+_n
> by make: gen annrep=strtoname("rep"+string(77+_n),1) if Year==1977+_n
>
> which returns 'rep78' rather than the value from column 'rep78'.

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


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index