### Sunday, March 19, 2006

## RE: st: Looping question

I'd go further than Maarten. In essence, this data structure will remain awkward. Almost whatever you did that requires reference to these "friends" will require the same complicated transform, do and back-transform sequence.

reshape long frnd , i(aid) drop if missing(frnd)

is sufficient to get to a better structure.

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

Maarten buis

> The trick is a combination of -reshape- and -merge-. See example. > *---------------begin example------------- > drop _all > tempfile a > input frnd delinq > 99 2 > 88 3 > 77 5 > 66 2 > 55 6 > 44 8 > end > sort frnd > save `a', replace > > drop _all > input aid frnd1 frnd2 frnd3 frnd4 > 99 66 77 . . > 88 77 99 . . > 77 55 44 99 > 66 88 99 44 77 > 55 44 . . . > 44 66 . . . > end > > reshape long frnd, i(aid) > drop if frnd ==. > sort frnd > merge frnd using `a' > drop if _merge == 2 /*remove people who are nobodys friend*/ > drop _merge > reshape wide frnd delinq, i(aid) j(_j) > list > *---------------------end example-----------------------

Carter Rees

> > I am working with a data set in which cases are students > each of which has a > > unique identifier aid. Also, for each student I have a > delinquency variable > > (delinq) which is a count of the number of delinquent acts they have > > committed over the past 12 months. Each student has > nominated up to 10 > > friends which are represented by the variable vector > frnd1-frnd10. The > > values of frnd1-frnd10 are the aid's of each of the > nominated friends. What > > I would like to do is create a variable vector > fr1del-fr10del which takes on > > the delinquency value for each of the nominated friends. > > > aid delinq frnd1 frnd2 frnd3 frnd4 frnd5 ... > > 99 2 66 77 > > 88 3 77 99 > > 77 5 55 44 99 > > 66 2 88 99 44 77 > > 55 6 44 > > 44 8 66 > >

Working with Stata 9.1, Windows XP.

I am working with a data set in which cases are students each of which has a unique identifier aid. Also, for each student I have a delinquency variable (delinq) which is a count of the number of delinquent acts they have committed over the past 12 months. Each student has nominated up to 10 friends which are represented by the variable vector frnd1-frnd10. The values of frnd1-frnd10 are the aid's of each of the nominated friends. What I would like to do is create a variable vector fr1del-fr10del which takes on the delinquency value for each of the nominated friends.

I have been exploring the reshape, foreach, and forvalues commands but have not had much luck to date.

aid delinq frnd1 frnd2 frnd3 frnd4 frnd5 ... 99 2 66 77 88 3 77 99 77 5 55 44 99 66 2 88 99 44 77 55 6 44 44 8 66

* * For searches and help try: * http://www.stata.com/support/faqs/res/findit.html * http://www.stata.com/support/statalist/faq * http://www.ats.ucla.edu/stat/stata/

Tag: statalist