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: foreach loop for scoring answers

From   Stas Kolenikov <>
Subject   Re: st: foreach loop for scoring answers
Date   Mon, 21 Jun 2010 21:54:29 -0400

If you have "wide" data (in the sense of -reshape-), you can

sort id
foreach x of <the list of items> {
   gen byte correct_`x' = (`x'==`x'[_N])

If you have "long" data,

bysort item (id) : gen byte correct = (response == response[_N])

On Mon, Jun 21, 2010 at 7:51 PM, Brandon Olszewski
<> wrote:
> I have a data set containing answers from 100 student answers to a
> 50-item test; one of the "students" is the answer key (id=101). I want
> to create new variables coded 0/1 (incorrect/correct) by comparing
> student answers to the key's answers. I realize I could do this
> somewhat barbarically, like this:
> gen correct_1=<manually enter correct answer for question 1>
> gen dummy_1=0
> replace dummy_1=1 if answer_1==correct_1
> ...and so on, copying syntax and replacing '1' with whatever number
> question I'm working with.
> Instead, I want to do this more elegantly by writing a macro that will
> do this for me. I understand that I can use the foreach or forvalues
> commands to accomplish this, but am lost on how to get started with
> them, as my unix language skills and familiarity with macros are not
> up to par. I'm also unsure about how to use one of my cases (the key,
> id=101) in the comparative way I've described (to create values for
> in/correct answers from the 100 other cases). I have checked previous
> do-files (that colleagues have built with me) and stata documentation
> but am unable to make solid sense out of them for my purposes.
> Any help is appreciated.
> Best,
> Brandon Olszewski
> *
> *   For searches and help try:
> *
> *
> *

Stas Kolenikov, also found at
Small print: I use this email account for mailing lists only.

*   For searches and help try:

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