Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: how to create groups


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: how to create groups
Date   Tue, 24 Sep 2002 17:11:05 +0100

Radu Ban
> 
> I translated an excel dataset into Stata and now it looks like this
> 
> Group1        obs1
> Subgroup1    obs2
> <blank>        obs3
> <blank>        obs4
>   .........        .........
> <blank>       obsn1
> Group1        obs1
> Subgroup2    obs2
> <blank>        obs3
> ...............    ........                   
>  <blank>     obsn2
> ............................
> Groupk        obs1
> Subgroupl    obs2
> <blank>       obs3
>                     .....
> <blank>       obsns
> 
> I would like to transform this into:
> Group1 Subgroup1         obs1
> Group1 Subgroup1         obs2
> .......... ..................
> Group1 Subgroup2        obs
> Group1 Subgroup2        obs
> ..........
> Group1 Subgroup_n        obs
> Group2 Subgroup_n+1    obs
> Group2 Subgroup_n+1    obs
> ..............................
> 
> 
> I know I could just do the changes in excel by copying and dragging
> but I would like to learn how to do it in Stata.
> 

I am going to interpret this as two Stata variables
called -what- and -value-. 

First, we pull out the Group identifiers. 
One way to select them is that the previous 
value of -what- is missing. (This holds for the
very first as well.) 

. gen Group = what if missing(what[_n-1]) 

Very likely this will need to be 
a string variable, i.e. 

. gen str1 Group = "" 
. replace Group = what if missing(what[_n-1]) 

Now we pull out the Subgroup identifiers. 

. gen Subgroup = what if missing(Group) 

or as a string variable 

. gen str1 Subgroup = "" 
. replace Subgroup = what if missing(Group) 

Subgroup needs to be copied upwards to the first 
line of each Group 

. replace Subgroup = Subgroup[_n+1] if !missing(Group) 

Now we fill in the blanks in a downwards cascade 

. replace Group = Group[_n-1] if missing(Group) 
. replace Subgroup = Subgroup[_n-1] if missing(Subgroup) 

The reasoning is spelled out at 
http://www.stata.com/support/faqs/data/missing.html

I hope that helps. 

Nick 
n.j.cox@durham.ac.uk 
*
*   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