Statalist


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

Re: st: from long to wide


From   "Austin Nichols" <austinnichols@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: from long to wide
Date   Wed, 31 Oct 2007 10:18:07 -0400

Simply generate new variables instead:

g obs=_n
bys hid pid (obs): gen n=_N
su n, meanonly
forv i=1/`r(max)' {
 by hid pid: g injdam`i'=injdam[`i']
}

then you can drop obs if you like:
 by hid pid: drop if _n>1

and drop now extraneous vars:
 drop n injdam

Here's a cut-and-paste-able comparison:

clear
input str1 hid pid str1 injdam
        A  1  a
        A  2  b
        A  2  c
        B  1  b
        B  2  a
end
g obs=_n
bys hid pid (obs): g n=_n
egen g=group(hid pid)
drop obs
reshape wide injdam, i(g) j(n)
drop g
li, noo sepby(hid)

clear
input str1 hid pid str1 injdam
        A  1  a
        A  2  b
        A  2  c
        B  1  b
        B  2  a
end
g obs=_n
bys hid pid (obs): gen n=_N
su n, meanonly
forv i=1/`r(max)' {
 by hid pid: g injdam`i'=injdam[`i']
}
by hid pid: drop if _n>1
drop obs n injdam
li, noo sepby(hid)

On 10/31/07, N.Li@ms.unimelb.edu.au <N.Li@ms.unimelb.edu.au> wrote:
> How to reshape a data set from long form to wide form without using the
> command reshape?
> (Recently I am working on a data set from a remote access data library.
> The RADL receives STATA codes submitted by its users, it runs jobs and
> returns outputs via its web interface.  For confidentiality reasons, not
> all the STATA commands are allowed in the RADL. One of these commands is
> reshape,  we know reshape a dataset won't reveal extra information though.
>  I tried to program but failed.  )
*
*   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