Statalist


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

st: RE: Manipulating dataset in stata


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: Manipulating dataset in stata
Date   Tue, 29 Jul 2008 19:20:24 +0100

Assuming that your identifiers run 1 up, then here is one way: 

local N = _N 
expand `N' 
bysort ID : gen ID2 = _n 
gen DeltaN = . 

qui forval i = 1/`N' { 
	su N if ID == `i', meanonly 
	replace DeltaN = N - r(mean) if ID2 == `i' 
} 

If your identifiers do not run 1 up, use -egen, group()- to get new
identifiers that do. 

Also note 

FAQ     . . . . . . . . . . . . . . . Expanding datasets to all possible
pairs
         2/03    How do I produce a dataset based on all possible
                pairs of identifiers within each group?
                http://www.stata.com/support/faqs/data/pairs.html

Nick 
[email protected] 

Cesare Fracassi

I have a db that looks like this:

ID   N
1      1
2      0
3      4
4      2
5      2
6      0
.........
10     1


(the actual db is much larger than that)

I would like to create a new dataset that contains the nxn possible 
combinations of observations and takes the differences between the Ns:

ID1   ID2   DeltaN
1         1          0 
1        2          1
1        3         -3
1        4         -1
........................
1      10         0
2         1         -1
2         2         0
2          3         -4
.......

and so on for the nxn possible combinations...

any idea how to code that in 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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index