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

# Re: st: Label values with a range

 From "Friedrich Huebler" To statalist@hsphsun2.harvard.edu Subject Re: st: Label values with a range Date Fri, 8 Feb 2008 12:54:10 -0500

```The approach suggested by Maarten leads to an error message in some
cases. Let's divide price by 1000 and adjust the code accordingly.

sysuse auto, clear
replace price = price/1000
levelsof price
foreach lev in `r(levels)' {
local lab = cond(inrange(`lev', 3, 4), "3-4", ///
cond(inrange(`lev', 4, 5), "4-5", ///
">5"))
label define price `lev' "`lab'", add
}
label value price price
tab price

Stata returns this error message:

may not label 3.290999889
r(198);

The original poster, Ben, wanted to label his values but both Maarten
and Sergiy (who proposed -recode-) indicated in their replies that
other solutions may be better. I would also suggest generating a new
variable that indicates the range of the original variable. The
commands below have the added advantage that no specific ranges have
to be spelled out. As long as there is at least one observations
between 0 and 10, 10.1 and 20, 20.1 and 30, and so on, the resulting
groups will be numbered consecutively from 1 upwards.

sysuse auto, clear
replace displacement = displacement/10
gen dgroup = ceil(displacement/10)
levelsof dgroup
foreach l in `r(levels)' {
lab def dgroup `l' "Group `l'", add
}
lab val dgroup dgroup
tab dgroup

dgroup |      Freq.     Percent        Cum.
------------+-----------------------------------
Group 1 |         14       18.92       18.92
Group 2 |         26       35.14       54.05
Group 3 |         19       25.68       79.73
Group 4 |         14       18.92       98.65
Group 5 |          1        1.35      100.00
------------+-----------------------------------
Total |         74      100.00

Friedrich

On Feb 6, 2008 10:00 AM, Maarten buis <maartenbuis@yahoo.co.uk> wrote:
> --- Benjamin Allaire <btallaire@hotmail.com> wrote:
> > I would like to label the values in my dataset with a range (instead
> > of having to make an entirely new variable)
> >
> > For example, all values between 1 and 10 (not just integers) would be
> > labeled group I, 10.1-20 would be group II, etc.
> >
> > Is there a way to do this?
>
> Yes it can be done, but I don't think it is particularly useful:
>
> *---------------- begin example ---------------
> sysuse auto, clear
> levelsof price
> foreach lev in `r(levels)' {
>         local lab = cond(inrange(`lev', 3000, 4000), "3000-4000", ///
>                   cond(inrange(`lev', 4000, 5000), "4000-5000", ///
>                         ">5000"))
>         label define price `lev' "`lab'", add
> }
> label value price price
> tab price
> *------------------ end example ----------------
*
*   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–2015 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index