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]

Re: st: Another question regarding string variables


From   Chamara Anuranga <kcanuranga@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Another question regarding string variables
Date   Thu, 28 Feb 2013 13:47:32 +0530

Dear Anwar,
Following are the command to do that for a string variable called
stringvar. This is not the straight forward method. Somebody may be
able to come up with easy method. however you can use following codes.
replace stringvar from your variable name. check how many new variable
created from split command. then  change the code accordingly.

Thanks,
Chamara

*Split the name
split stringvar,gen(newstr)
*Check the last word
	gen numword=0
	local i=1
foreach var of varlist `r(varlist)' {
	replace numword=numword+1 if `var'~=""
	gen length`i'=length(`var')
	local i=`i'+1
}

foreach num of numlist 2/6 {
	replace newstr`num'="" if length`num'==1 & numword==`num'
}
	gen wordnew=newstr1+" "+newstr2+" "+newstr3+" "+newstr4+" "+newstr5+" "+newstr6
	replace wordnew=trim(wordnew)










On Thu, Feb 28, 2013 at 12:11 PM, Dudekula, Anwar <dudekulaan@upmc.edu> wrote:
> Hi
>
> One quick question for the readers.
> Is it possible to trim last word if it is one alphabet wide like removing. A from   ( Graham , Tony   A )
>
>
> Sincerely,
> Anwar
>
> On Feb 27, 2013, at 12:01 AM, "Steve Nakoneshny" <scnakone@ucalgary.ca> wrote:
>
>> Hi Mike,
>>
>> I don't have access to the help file from my phone, but I'm fairly certain you should be able to extract *any* word from a string var using the -word- function.
>>
>> Completely untested off the top of my head (with no recollection of the appropriate syntax):
>>
>> g lname = word(yourvar,1)
>> g fname = word(yourvar,2)+word(yourvar,3)
>>
>> The above is an inelegant means of approximating your needs. Adjusting for valid syntax would be a good start. I have no doubt that there are other string function solutions that would equally suffice.
>>
>> If you are wedded to using -split-, you may with to insert a comma between words 1 & 2 of your string via -subinstr- and then proceed with -split yourvar,parse(,)-.
>>
>> Steve
>>
>> Sent via carrier pigeon
>>
>> On 2013-02-26, at 9:31 PM, "Michael Stewart" <michaelstewartresearch@gmail.com> wrote:
>>
>>> HI Steve
>>>
>>> Word will give me the second word
>>>
>>> But What I am trying to get is the first word and rest of the string
>>> as second variable.
>>>
>>> For ex: John Howard R --> John  & (Howard R ) as two string AND not as
>>> John & Howard & R  separately  as three string
>>>
>>> Thanks
>>> MIke
>>>
>>>
>>> On Tue, Feb 26, 2013 at 11:14 PM, Steve Nakoneshny <scnakone@ucalgary.ca> wrote:
>>>> There is a string function called -word- that will serve your purpose. See -h word- for more details.
>>>>
>>>> Steve
>>>>
>>>> Sent via carrier pigeon
>>>>
>>>> On 2013-02-26, at 8:55 PM, "Michael Stewart" <michaelstewartresearch@gmail.com> wrote:
>>>>
>>>>> 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/
>>>
>>>
>>>
>>> --
>>> 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/
>>
>
> *
> *   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