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 at the end of May, and its replacement, statalist.org is already up and running.


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

Re: st: reshaping to wide format, and need to create a "j" variable


From   "Dimitriy V. Masterov" <dvmaster@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: reshaping to wide format, and need to create a "j" variable
Date   Tue, 25 Oct 2011 17:35:43 -0400

Kendra,

I don't think that will work. For example,

bys id: egen totplacements=count(placement)

will count the number of times the id number appears. But this will
not allow you to reshape as there will be multiple observations at the
same totplacements within id.

You need to fix the placement variable, which currently does not make
sense to me. Assuming your date is correct, try the following code:

/* Fix Date and placement variables */
gen dop=date(date_of_placement,"MDY") /* or DMY if that makes sense */
format dop %td
drop placement;
bys id (date): gen placement=_n

reshape wide age_at_placement dop, i(id) j(placement)

DVM


On Tue, Oct 25, 2011 at 5:03 PM, Kendra Lewis <lewiske@onid.orst.edu> wrote:
> Dear all,
>
> I have data that tracks foster children's transitions and placements.
> The data is in long format, such that children have the same ID
> variable over time, but their ID variable appears in several different
> rows. Each time their ID variable appears indicates a move or
> transition. Here is an example dataset:
>
> id           placement     date of placement    age at placement
> 1              1                  6.7.2009                       14
> 1              2                  8.2.2010                       15
> 1              3                  2.3.2011                       15
> 1              1                  3.4.2011                       15
> 2              1                  5.4.2009                       12
> 3              1                  4.6.2009                       13
> 3              2                  7.8.2010                       14
> 4              1                  4.5.2009                       10
> 4              2                  6.7.2009                       10
> 4              3                  5.2.2010                       11
> 4              2                  7.8.2010                       11
> 4              3                  9.9.2010                       12
> 4              1                  1.4.2011                       12
> 5              1                  7.8.2009                       13
> 5              2                  6.4.2010                       14
>
> So, id #1 has had 4 placements, #2 has only had 1, #3 as had 2, and so
> on. The data needs to be put into wide format to merge in other
> datasets of a similar format. I know the necessary command will be
> reshape wide stub, i(id) j(??)
> where stub will be the varlist-as there is no common stub in the
> dataset. What I need is a "j" variable that indicates a count of the
> number of times the id number appears. For example, the count for id
> #1 is 4, for id #2 is 1, for id #3 is 2, for id #4 is 6, and for id #5
> is 2. Then I can use this for the reshape command to be my "j"
> variable.
> Does anyone have any suggestions? I've spoken to a few people and we
> think it may be some sort of "foreach" loop command but we are not
> sure.
>
> Thanks!
>
> Kendra
>
> *
> *   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/
>

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