Bookmark and Share

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

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

Re: st: the use of loop

From   Steve Nakoneshny <>
To   "" <>
Subject   Re: st: the use of loop
Date   Wed, 11 Apr 2012 14:58:22 -0600

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


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

*   For searches and help try:

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