Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


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

RE: st: foreach loop for scoring answers


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   RE: st: foreach loop for scoring answers
Date   Tue, 22 Jun 2010 10:00:13 +0100

Tutorial articles on -for*- loops and -by:- are accessible to all
through the Stata Journal website: 

SJ-2-2  pr0005  . . . . . .  Speaking Stata:  How to face lists with
fortitude
        Q2/02   SJ 2(2):202--222                                 (no
commands)
        demonstrates the usefulness of for, foreach, forvalues, and
        local macros for interactive (non programming) tasks

SJ-2-1  pr0004  . . . . . . . . . . Speaking Stata:  How to move step
by: step
        Q1/02   SJ 2(1):86--102                                  (no
commands)
        explains the use of the by varlist : construct to tackle
        a variety of problems with group structure, ranging from
        simple calculations for each of several groups to more
        advanced manipulations that use the built-in _n and _N

Nick 
n.j.cox@durham.ac.uk 

Stas Kolenikov

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

> 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.

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