Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: reshape when the stub is at the beginning of the variables


From   "Neil Shephard" <nshephard@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: reshape when the stub is at the beginning of the variables
Date   Wed, 26 Apr 2006 09:23:58 +0800

> Quoting Carol Bao <yanjunbb@yahoo.com>:
>
> > Hi, I am asking this for a friend:
> >
> > She has a data set in its wide form like this:
> >
> > ID   Wave1 Wave2 Wave3 ASMOK BSMOK CSMOK
> > 1     0      1     1    .      1     0
> > 2     1      1     0    1      0     .
> >
> > Which reads: for person 1, he/she appears in wave 2
> > and wave 3 of the survey and reports to smoke in wave
> > 2 (BSMOK) and not smoke in wave 3 (CSMOK).  For person
> > 2, he/she is interviewed in wave 1 and 2 but drops out
> > in wave 3 and reports to smoke in wave 1 and not in
> > wave 2.
> >
> > The question is, the stub for reshape into variable
> > SMOK appears at the beginning of the variables as A, B
> > and C respectively for three waves (all the other
> > variables as well).  Is there any easier way to
> > reshape the data? Or any suggestions?
> >

My inital thought was this could probably be achieved using -renvars-
might be of use here, but whilst its does many things with both
prefixes and suffixes, I couldn't see how to use it to achieve this.

A quick hack resulted in the following code which should be easily
adaptable to the original problem....

clear

* Generate some variables with prefix stubs
set obs 10
foreach x in a b c d e f{
  gen `x'something = .
}
describe

* Now rename them so that the stub is at the end

unab orig: *something
foreach x in `orig'{
  local y: subinstr local x "something" ""
  rename `x' something`y'
}
describe

HTH's

Neil

--
"Smoking is one of the leading causes of statistics." - Fletcher Knebel

Email - nshephard@gmail.com / neilshep@cyllene.uwa.edu.au
Website - http://slack.ser.man.ac.uk/
Blog - http://slack---line.blogspot.com/
Flickr - http://www.flickr.com/photos/slackline/

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



© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index