Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# Re: st: problems with using preserve in looping

 From ken first To statalist@hsphsun2.harvard.edu Subject Re: st: problems with using preserve in looping Date Fri, 24 May 2013 16:50:39 +0100

```my question in fact is very similar to this one
http://www.stata.com/statalist/archive/2009-06/msg00926.html
but i am not sure how it works.

On 24 May 2013 16:24, ken first <kenscholar2008@gmail.com> wrote:
> it really needs to delete other years in order to allow the matrix to
> work, but i would like to keep the results it generates for every set
> of periods.
>
> On 24 May 2013 16:20, ken first <kenscholar2008@gmail.com> wrote:
>> thanks...however i am using a common (not gen) that can not combine with by.
>>
>> On 24 May 2013 16:12, William Buchanan <william@williambuchanan.net> wrote:
>>>
>>> bys year: g B=A*matrix if inrange(year,1991,2011)
>>>
>>>
>>>
>>> Sent from my iPhone
>>>
>>> On May 24, 2013, at 10:55, ken first <kenscholar2008@gmail.com> wrote:
>>>
>>>> Dear stata users,
>>>>
>>>> I have two questions here and i hope someone can help.
>>>>
>>>> looping command. I used the below. sometimes it works and everything
>>>> runs smoothly and sometimes i get an error message. Does anyone know
>>>> what i did wrong here?
>>>>
>>>> first i have
>>>>
>>>> predict A, e
>>>>
>>>> then
>>>>
>>>> forvalue year = 1991/2011 {
>>>> preserve
>>>> keep if  year==`year'
>>>> gen B=A*matrix
>>>> restore
>>>> }
>>>>
>>>> r(621);
>>>>
>>>>
>>>>
>>>> 2. I need to use the below as there is a matrix that wont work if i
>>>> simply use if year==`year', but i would like to keep the results I
>>>> generated after everything has been restored. does anyone know what to
>>>> do?
>>>>
>>>>
>>>> first i have
>>>>
>>>> predict A, e
>>>>
>>>> then
>>>>
>>>> forvalue year = 1991/2009 {
>>>> preserve
>>>> keep if  year==`year'
>>>> gen A=B*matrix   * i would like to keep the result here as a separate variable.
>>>> restore
>>>> }
>>>>
>>>> Many thanks
>>>>
>>>> Ken
>>>> *
>>>> *   For searches and help try:
>>>> *   http://www.stata.com/help.cgi?search
>>>> *   http://www.stata.com/support/faqs/resources/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/faqs/resources/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/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/
```