Statalist


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

RE: st: "sort" over different variables - thanks Nick and Martin


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   RE: st: "sort" over different variables - thanks Nick and Martin
Date   Tue, 25 Aug 2009 14:00:45 +0100

I agree largely with Niels. 

Although there may well be a -rowsort- solution, with these data I would recommend instead creating and staying within a different data structure. The moderately unfashionable -stack- is here a transparent alternative to -reshape-. 

stack ID M1 Birth_dateM1 ID M2 Birth_dateM2 ID M3 Birth_dateM3, into(ID M Birth_date) clear 

sort ID M 

then gets the order you want. 

I wouldn't -reshape- back. This data structure makes many things easier. 

Nick 
[email protected] 

Niels Schenk

This should do the trick:

reshape long M Birth_dateM, i(ID) j(suf)
bys ID (M): replace suf = _n 
reshape wide M Birth_dateM, i(ID) j(suf)

On Tue, 25 Aug 2009 12:28:07 +0200, "[email protected]"
<[email protected]> wrote:
> Thanks Nick and Martin for your answers and help. 
> 
> Rowsort is a great tool and could be one solution (by the way it will
help
> me with other problems not mentioned here). However, I will describe my
> (original) problem in greater detail and describe why rowsort may not be
> the best solution (sorry for the earlier (and poor) description of my
> problem):
>  
> My dataset contains detailed information on organizational members (e.g.
> their date of entry). However, the dataset is not well structured, i.e.
in
> some cases there is a member1 and a member3 while a member 2 is missing.
In
> these cases, member3 is obviously member2. At this point, rowsort would
> help me fixing the problem. Using rowsort member1 would be the member
that
> entered the organization at first, member2 would be the member that
entered
> the organization at second, and so on.
>  
> However, my dataset includes additional information about the members,
as
> for example, their birth date. If I use rowsort to “rank” the members of
> the organizations, birth dates do not fit any longer. Additionally, I
could
> not use rowsort to rank the birth dates, since the member that entered
> first may not be the youngest.
>  
> My dataset looks like this (still simplified…). M1, M2 and M3 are the
> times of entry of the members.
>  
> ID   M1   M2   M3   Birth_dateM1   Birth_dateM2   Birth_dateM3
> 1     100   50    200          1970                 1980                

> 1960                  
> 2      ...
> 3     …
>  
> And should look like this:
>  
> ID   M1   M2   M3   Birth_dateM1   Birth_dateM2   Birth_dateM3
> 1      50   100   200          1980                 1970                

> 1960                  
> 2      ...
> 3     …
>  
>  
> Do you have any idea how to fix this problem (is there a solution within
> rowsort)?   

*
*   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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index