Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | Austin Nichols <austinnichols@gmail.com> |
To | statalist@hsphsun2.harvard.edu |
Subject | Re: st: Populating time series records with values from different "Year" columns (rep78, rep79...) |
Date | Wed, 21 Dec 2011 16:53:46 -0500 |
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/