Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: From: Richard Moverare <richard.moverare@gmail.com>


From   Sergiy Radyakin <serjradyakin@gmail.com>
To   "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu>
Subject   Re: st: From: Richard Moverare <richard.moverare@gmail.com>
Date   Mon, 17 Jun 2013 17:04:16 -0400

Perhaps something like this? All list commands are for illustration
only, drop them from the production code when you understand how the
example works.
Best, Sergiy


clear
input str27 a
"234 Used cars, no batteries"
"22 Bottles - unspecifed"
"234 Used cars, no batteries"
"440 N/A"
end

list

gen first=substr(a,1,strpos(a," ")-1)
gen last=substr(a,strpos(a," ")+1, strlen(a)-strpos(a," "))

destring first, replace

list


preserve
duplicates drop a, force
sort first
local cmd "label define mylbl "
forval i=1/`=_N' {
  local cmd `"`cmd' `=first[`i']' `"`=last[`i']'"'"'
}
restore

`cmd'
label values first mylbl
list

drop last
label list
list



On Sun, Jun 16, 2013 at 2:16 PM,  <owner-statalist@hsphsun2.harvard.edu> wrote:
> Dear Statalist,
>
> 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!
>
> All the best, Rick
> *
> *   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index