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: st: Defining upper limit of loop


From   Nabin Kafle <[email protected]>
To   "[email protected]" <[email protected]>
Subject   Re: st: Defining upper limit of loop
Date   Wed, 11 Dec 2013 17:11:05 -0600

Sarah,

I want the following result to happen.

gen sum=X+X[_n+1] if Z==1
replace sum=X+X[_n+1]+X[_n+2] if Z==2
replace sum=X+X[_n+1] +X[_n+2] + X[_n+3] if Z==3
replace sum=X+X[_n+1] +X[_n+2] + X[_n+3]+X[_n+4] if Z==4



On Wed, Dec 11, 2013 at 5:03 PM, Sarah Edgington <[email protected]> wrote:
> Nabin,
> It looks like you want Stata to do a different thing depending on the value
> of Z for a given observation.  For a given observation, however, you don't
> appear to want to do multiple things.
>
> For your simple example you could do this without a loop
> gen sum=X+X[_n+1] if Z==1
> replace sum=X+X[_n+2] if Z==2
> replace sum=X+X[_n+3] if Z==3
> replace sum=X+X[_n+4] if Z==4
>
> Of course a loop for something that repeats like this helps prevent typos
> and is going to be helpful if you have a lot of values of Z to deal with.
>
> To make it a loop you could do something like:
> gen sum=0
> sum Z
> forv i=1/`r(max)' {
>         replace sum =X+X[_n+`i'] if Z==`i'
> }
>
> I suspect your actual task is probably more complicated than your example,
> in which case this may or may not get you where you're going.
>
> In general, though, when working with loops I often find it's illuminating
> for me to write out exactly what I want to happen for the first couple
> iterations of the loop.  If I can get the results I want for those first few
> iterations, that often helps make it very clear what the final loop needs to
> look like.
>
> -S
>
>
> -----Original Message-----
> From: [email protected]
> [mailto:[email protected]] On Behalf Of Thomas, Anthony
> Sent: Wednesday, December 11, 2013 2:44 PM
> To: [email protected]
> Subject: Re: st: Defining upper limit of loop
>
> Yes, a forval loop will work for that. Nick's explanation describes how very
> well. I had thought you wanted to actually reference a variable's name (i.e.
> a string). Just be aware that the macro `Z'
> needs to evaluate to numeric not string. Sorry for the confusion.
>
> Anthony
>
> On Wed, Dec 11, 2013 at 5:30 PM, Nabin Kafle <[email protected]> wrote:
>> I want to loop a cycle number of times indicated by a number in a variable
> name.
>> For example:
>> X Y Z
>> 1 2 1
>> 2 3 4
>> 3 2 2
>> 3 2 2
>> 4 2 2
>> . . . .
>>
>> gen sum=X
>> forvalues i=1/"Z" {
>> replace sum=X+X[_n+`i']
>> }
>>
>> What would be other way to do such if it is not possible by -forvalues-
> command.
>>
>> On Wed, Dec 11, 2013 at 4:18 PM, Thomas, Anthony
>> <[email protected]> wrote:
>>> Stata probably will not let you do that. Variable names cannot be a
>>> number, and "forval" type loops expect a numeric range (i.e.
>>> "variable name" would have to be a number). If you want to use
>>> variables in a loop, consider using:
>>>
>>> foreach i of varlist "varlist"{
>>>
>>>      commands
>>>
>>> }
>>>
>>> Anthony
>>>
>>> On Wed, Dec 11, 2013 at 5:08 PM, Nabin Kafle <[email protected]> wrote:
>>>> Is there a way that I can use the forvalues command in stata in a
>>>> way that I upper value of the loop can be defined from the varibale
> list.
>>>>
>>>> forvalues i = 1/"var name" {
>>>> code for loop
>>>> *
>>>> *   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/
>>
>>
>>
>> --
>> Nabin Kafle
>> *
>> *   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/



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


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index