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 on April 23, and its replacement, statalist.org is already up and running.


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

Re: st: creating variables using 'by' for subsets of records


From   Eric Booth <ebooth@ppri.tamu.edu>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: creating variables using 'by' for subsets of records
Date   Tue, 23 Feb 2010 14:27:18 -0600

>

Something like this:

****************BEGIN EXAMPLE
/*
I have records for individuals by geographic region and wish to aggregate
the records for individuals to records for geographic regions.  I believe I
should create variables for those regions using 'by'. 
*/
clear

inp indiv str6 region indicator
1 "north" 6 
2 "north" . 
3 "north" .
4 "south" 5
5 "south" 5
6 "south" .
7 "east" 3 
8 "east" 9
9 "east" 0 
10 "east" 6
end

egen region_code = group(region)
*or use -encode-*

/*
...to create a region variable that expresses a ratio of
count of indicator values for individuals in a region to a count of
individuals in the region and am unsure how to code this.
*/

foreach v in indicator indiv {
	bys region: egen ind_`v' = count(`v')
	label var `v' "Count of `v'"
	}
*ratio*
g ratio = ind_indicator/ind_indiv
li ind* ratio
****************END EXAMPLE

~ Eric
__
Eric A. Booth
Public Policy Research Institute
Texas A&M University
ebooth@ppri.tamu.edu
Office: +979.845.6754


On Feb 23, 2010, at 1:55 PM, John Westbury wrote:

> I have records for individuals by geographic region and wish to aggregate
> the records for individuals to records for geographic regions.  I believe I
> should create variables for those regions using 'by'.  Ex: by Region gen x =
> argument for variable.  I am having difficulty with arguments for variable
> x.  For example I wish to create a region variable that expresses a ratio of
> count of indicator values for individuals in a region to a count of
> individuals in the region and am unsure how to code this.




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