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 on April 23, and its replacement, statalist.org is already up and running.


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

st: RE: Another question regarding string variables


From   Kieran McCaul <kieran.mccaul@uwa.edu.au>
To   "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu>
Subject   st: RE: Another question regarding string variables
Date   Wed, 27 Feb 2013 12:58:13 +0800

....

You can do this with regular expressions:

clear *
input str20 name
"John Howard R"
end
gen first = regexs(1) if regexm(name, "([a-zA-Z]+)[ ]*([a-zA-Z]+)[ ]*[a-zA-Z]")
gen last = regexs(2)+ " " +regexs(3)  if regexm(name, "([a-zA-Z]+)[ ]*([a-zA-Z]+)[ ]*([a-zA-Z])")
list

or you could do it with the word() function:

clear *
input str20 name
"John Howard R"
end
gen first = word(name,1)
gen last = word(name,2)+ " " +word(name,3)
list



-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Michael Stewart
Sent: Wednesday, 27 February 2013 11:55 AM
To: statalist
Subject: st: Another question regarding string variables

Hi,

I  am sorry to keep bothering you regarding string variables
I am trying to find if there is a function to split a  string "Howard
James R" --> "Howard"  & ("James R")
If I use Split, I would get Howard, James and R which is not what I want
I want to split the string after the first word  into two string
variables  first variable containing first word and second variable
containing rest of the string

-- 
Thank you ,
Yours Sincerely,
Mike.
*
*   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