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]

numeric value and value label from string [was: Re: st: From: Richard Moverare ....]


From   Nick Cox <[email protected]>
To   "[email protected]" <[email protected]>
Subject   numeric value and value label from string [was: Re: st: From: Richard Moverare ....]
Date   Sun, 16 Jun 2013 22:16:38 +0100

gen numvar = real(word(myvar, 1))
assert numvar == ceil(numvar) & numvar < .

gen lblvar = substr(myvar, strpos(myvar, " "), .)

Then find -labmask- (SJ)

labmask numvar, values(lblvar)

Nick
[email protected]

Richard Moverare

>
> I have a small problem that I guess I should be able to solve, but I
> got stuck. I have a string variable that consists of 1-3 numbers,
> space, and then a description. The description varies in terms of
> numer of words and characters.
>
> What I would like to do is to create a numeric variable where the
> values are the numbers in the beginning of the string and then use the
> rest of the string as a value label. The dataset includes a number of
> duplicates for this specific variable (one of the reason for doing
> this conversion is to make the dataset smaller in size). The first
> part is no problem using -word-, and I can get what I want as a label
> by getting the second word and those that follow, but I got stuck
> defining the labels for the variable.
>
> The variables looks something like this:
> 234 Used cars, no batteries
> 22 Bottles - unspecifed
> 234 Used cars, no batteries
> 440 N/A
> and so on.
>
> Any help would be greatly appreciated!
*
*   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