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: RE: RE: left right string


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   RE: st: RE: RE: left right string
Date   Wed, 19 May 2010 16:23:49 +0100

Martin gave good answers to the original question, but from this end of the thread it is clear that the real question is different. 

Tyler wants to go from a existing numeric variable with value labels to new numeric variables based on those labels. 

If that is it, then you could just look at the labels and go (e.g.) 

gen age_min = cond(hhh_age == 1, 15, 
              cond(hhh_age == 2, 20, 
		  cond(hhh_age == 3, 25)))

OR 

gen age_min = 15 * (hhh_age == 1) + 20 * (hhh_age == 2) + 25 * (hhh_age == 3) 

OR 

gen age_min = 15 if hh_age == 1
replace age_min = 20 if hh_age == 2 
replace age_min = 25 if hh_age == 3 

This is not an exhaustive list, no doubt. My point is just that you can look at the list of values and value labels and work out -generate- and -replace- code. 

Tyler's real example is no doubt a bit more complicated but the principle should apply. 

Nick 
[email protected] 

Martin Weiss

" I just realized my ranges are labels"

No big deal, just -decode- the thing beforehand:


***********
clear*

set obs 3
gen byte hhh_age=_n

la def ages 1 "15-19" 2 "20-24" 3 "25-39" 
la val hhh_age ages

decode hhh_age, gen(newhhh_age)

split newhhh_age, gen(age) parse(-) destring
rename age1 age_min
rename age2 age_max

list, noo
***********

Tyler Frazier

I just realized my ranges are labels

On Tue, May 18, 2010 at 11:08 PM, Martin Weiss <[email protected]> wrote:
>
> <>
>
>
> You can generate the new variables and -destring- (which I assume you also
> want) in one fell swoop:
>
>
> ***********
> clear*
>
> inp str5 hhh_age
> 15-19
> 20-24
> 25-39
> end
>
> split hhh_age, gen(age) parse(-) destring
> rename age1 age_min
> rename age2 age_max
>
> list, noo
> ***********

Martin Weiss

> Have you had a look at -split-?

Tyler Frazier

> simple question, I hope -- I have a variable which is a range of ages,
> for example  the variable name is hhh_age and the quantities are
> 15-19, 20-24, 25-39 etc...
>
> and I want to generate a new variable (age_min) that returns the left
> two characters from hhh_age , and another variable that generates the
> right two -- (age_max)
>
> I searched but couldnt find it, seems like a very simple thing, but
> cant figure it out.
>

*
*   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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index