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: the use of loop


From   shihying yao <[email protected]>
To   [email protected]
Subject   Re: st: the use of loop
Date   Wed, 11 Apr 2012 17:51:43 -0400

Thanks, Steve!

On Wed, Apr 11, 2012 at 4:58 PM, Steve Nakoneshny <[email protected]> wrote:
> Your loop crashes because you incorrectly specify your macro. At the most base level, the majority of your command is invariant; you really only want to cycle through values a b and c. Thus, specify your macro to only include the variant part of your command keeping the common portion together.
>
> One solution:
>
> foreach x in a b c {
>        gen Item1`x' = Item1`x'EB - Item1`x'AD
> }
>
>
> Steve
>
> On 2012-04-11, at 2:48 PM, shihying yao wrote:
>
>> Dear all,
>> I have a question regarding the use of loop in Stata 10, and would
>> appreciate any help.
>>
>> The data is composed of item scores from two graders. Each variable
>> represents a combination of a grader and an item. For example, the
>> variable "Item1aEB" represents scores of Item 1a by grader EB, and
>> "Item1aAD" represents Item 1a scores by grader AD. The goal is to
>> compare consistency in ratings (e.g., compare values of "Item1aEB" and
>> "Item1aAD").
>>
>> The problem I have now is how to do this comparison efficiently over
>> all items using a loop. For example, suppose I want to calculate the
>> difference in ratings (e.g. gen Item1a=Item1aEB - Item1aAD) repeatedly
>> for Items 1a,1b,and 1c. I tried "foreach" command
>>
>> foreach x of varlist Item1aEB- Item1cAD{
>> gen `x'= `x'EB - `x'AD
>> }
>>
>> This syntax is wrong because Stata will think variables are named as,
>> say "Item1aEBEB", rather than "Item1aEB." But I am not sure how to
>> correct this command. Any advice is appreciated!
>> *
>> *   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/

*
*   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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index