Bookmark and Share

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: egen cut - how to force a category even if zero observations


From   Sergiy Radyakin <serjradyakin@gmail.com>
To   "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu>
Subject   Re: st: egen cut - how to force a category even if zero observations
Date   Wed, 26 Feb 2014 19:45:04 -0500

Anthony, use -recode-.

sysuse nlsw88, clear
recode age (0/39=1) (40/49=2), generate(agecut)
sum age agecut

Best, Sergiy.

On Wed, Feb 26, 2014 at 7:38 PM, Anthony Khawaja
<anthonykhawaja@gmail.com> wrote:
> Is it possible to force the "egen cut" command to keep a specified category
> even though there are zero observations within that category.  For example,
> I want to write a script that will work on multiple different datasets, and
> I want to categorise age into <40, >=40 <50, >=50 <60, >=60 <70, >=70 <80,
>>=80 <90, >=90.  The egen cut command works well unless I have zero
> observation in a category - rather than still creating that as a level of
> the new categorical variable, Stata just doesn't form the category.  This
> would usually be fine, but I am using "file write" commands from which I
> want to produce identically shaped tab delimited files to easily overlay
> numbers from multiple studies.
>
> I have searched extensively but not found a simple solution.  Of course I
> could manually create the new variable, level by level.  But I have many
> such variables, and this would be time consuming (and lack elegance!).  Does
> anyone know of an elegant solution?
>
> For example, in one dataset, there are no participants <40 years.  So the
> following command yields one less level in the categorical variable produced
> than I wanted:
>
> . egen agecut = cut(age), at (0 40 50 60 70 80 90 130) label
>
>
> . tab agecut
>
>      agecut |      Freq.     Percent        Cum.
> ------------+-----------------------------------
>         40- |         29        0.39        0.39
>         50- |        873       11.73       12.12
>         60- |      3,540       47.56       59.67
>         70- |      2,364       31.76       91.43
>         80- |        629        8.45       99.88
>         90- |          9        0.12      100.00
> ------------+-----------------------------------
>       Total |      7,444      100.00
>
>
> Many thanks in advance for any advice.
>
> Anthony
>
>
>
> --
> View this message in context: http://statalist.1588530.n2.nabble.com/egen-cut-how-to-force-a-category-even-if-zero-observations-tp7580488.html
> Sent from the Statalist mailing list archive at Nabble.com.
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/faqs/resources/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/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index