Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | "David Radwin" <dradwin@mprinc.com> |
To | <statalist@hsphsun2.harvard.edu> |
Subject | RE: st: foreach loop for scoring answers |
Date | Tue, 22 Jun 2010 08:10:28 -0700 (PDT) |
You may also be interested in Kit Baum's paper "A little bit of Stata programming goes a long way," available at http://ideas.repec.org/p/boc/usug05/16.html , which introduces loops and other elements of Stata programming. David -- David Radwin Research Associate MPR Associates, Inc. 2150 Shattuck Ave., Suite 800 Berkeley, CA 94704 Phone: 510-849-4942 Fax: 510-849-0794 www.mprinc.com > -----Original Message----- > From: owner-statalist@hsphsun2.harvard.edu [mailto:owner- > statalist@hsphsun2.harvard.edu] On Behalf Of Nick Cox > Sent: Tuesday, June 22, 2010 2:00 AM > To: statalist@hsphsun2.harvard.edu > Subject: RE: st: foreach loop for scoring answers > > 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/