Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: st: histogram
From
Maarten buis <[email protected]>
To
[email protected]
Subject
Re: st: histogram
Date
Thu, 14 Oct 2010 15:14:25 +0100 (BST)
--- 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/