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: histogram


From   Schöler, Lisa <lschoeler@wiwi.uni-frankfurt.de>
To   "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu>
Subject   RE: st: histogram
Date   Thu, 14 Oct 2010 14:17:35 +0000

Thank you

-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Maarten buis
Sent: Thursday, October 14, 2010 4:14 PM
To: statalist@hsphsun2.harvard.edu
Subject: Re: st: histogram

--- On Thu, 14/10/10, Schöler, Lisa wrote:
> Now I need to get a histogram that uses only one
> observation for each id for the mean(brand). Therefore I
> wanted to include a dummy=1, so I can use the command 
> 
> . histogram if dummy==1
> 
> Can anybody help me how I can create a dummy=1 only for one
> observation per id? I was gonna use 
> 
> . by id: gen dup = cond(_N==1,0,_n)
> 
> and then
> 
> . histogram if dup==1
> 
> But the problem here is that I have missings within my id
> groups, so sometimes my dup per id starts with 2.

If you create your mean variable using -egen- then you won't
get missing values, thus preventing the entire problem.

Alternatively, you can ask Stata to sort within each id on
mean, and pick the first observation. A missing value is in
Stata a very large number, so they will in that case be the 
last observation.

*------------- begin example --------------
drop _all
input id   brand  year   
       1   7      1999 
       1   5      2000 
       1   3      2001 
       1   5      2002 
       1   .      2003 
       2   .      1999 
       2   7      2000 
       2   8      2001 
       2   5      2002 
       2   4      2003   
       3   .      1999 
       3   7      2000 
       3   8      2001 
       3   5      2002 
       3   4      2003 
 end
egen mean = mean(brand), by(id)
list
// no missing values, so that solves the problem

// lets create a missing value, and solve your
// problem than:
replace mean = . in 1
bys id (mean) : gen byte mark = _n == 1 if _N > 1
list
*------------------ end example ------------------

Hope this helps,
Maarten

--------------------------
Maarten L. Buis
Institut fuer Soziologie
Universitaet Tuebingen
Wilhelmstrasse 36
72074 Tuebingen
Germany

http://www.maartenbuis.nl
--------------------------


      

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