Statalist


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

Re: st: count cases of a string variable


From   Maarten buis <maartenbuis@yahoo.co.uk>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: count cases of a string variable
Date   Thu, 1 May 2008 11:14:04 +0100 (BST)

--- Tunga Kantarci <tungakantarci@hotmail.com> wrote:
> I have a variable that has string values. I was trying to count cases
> with the following command:
> 
> count if variablename == "string value"
> 
> which, however, returned me 0 cases.
>
> Then I converted the variable to a numeric one and then the count
> worked.
> 
> 
> After this two questions arise:
> 
> 1.  Why the command -count if variablename == "string value"- did not
> work? What instead should I have used?

This example could mean two things: 

1) You want to know how many observations have a string (i.e. not
missing). In that case this code won't work, it will look for
observations whose value is exactly "string value", and probably finds
none. In that you could do one of these two things (and there are
probably many more options):

*----------- begin example --------------
sysuse auto, clear
codebook make
count if !missing(make)
*------------ end example ---------------

2) You want to know how many observations have a specific string. In
that case your syntax is legal, but can be very sensitive to any typos,
this string has to match exactly what is in the variable (that is what
you asked Stata to do, so that is what you will get). If the spelling
is correct you will get your count:

*------------------- begin example -----------------
sysuse auto, clear
count if make == "Subaru" /*correct spelling*/
count if make == "subaru" /*wrong spelling*/
*------------------- end example -------------------
 
> 2. After I converted the variable, I command "describe", "tab",
> "sum"... I only see in the "sum variablename" command the min and max
> values stata has assigned but  I don't see a complete list of
> assigned numeric values to string values. Why and how can I see it?

You may want to look at Ben Jann's -fre- command downloadable from ssc,
type in Stata -ssc install fre-, folowed by -help fre-.

Hope this helps,
Maarten

-----------------------------------------
Maarten L. Buis
Department of Social Research Methodology
Vrije Universiteit Amsterdam
Boelelaan 1081
1081 HV Amsterdam
The Netherlands

visiting address:
Buitenveldertselaan 3 (Metropolitan), room Z434

+31 20 5986715

http://home.fsw.vu.nl/m.buis/
-----------------------------------------


      ___________________________________________________________ 
Yahoo! For Good. Give and get cool things for free, reduce waste and help our planet. Plus find hidden Yahoo! treasure 

http://green.yahoo.com/uk/earth-day/
*
*   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