Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

RE: st: Wildcard for string variables


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   RE: st: Wildcard for string variables
Date   Thu, 17 Jul 2008 14:27:55 +0100

Note that this is not an answer to the question as asked. 

-regexm("Tom and Tim", "Tim")- is 1. 

You could use -regexm(<whatever>, "^Tim ")- to catch the first word. 

It is a matter of taste, but it seems simpler to me to reach for regular
expression functions only when simpler functions such -substr()- and
-strpos()- don't yield easier answers. 

In fact -word()- is also available for the purpose. 

Nick
[email protected] 

John Wagai

To get to your final answer use the regex command:

gen seq = regexm(newvar, "Tim")

NB use the newvar generated by Rich below

-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Richard
Goldstein
Sent: 17 July 2008 16:04
To: [email protected]
Subject: Re: st: Wildcard for string variables

Sorry about the typo:

gen byte newvar=substr(sequence,1,4)=="Tim "

Richard Goldstein wrote:
> I don't see any need for a wildcard; how about
> 
> gen byte newvar=substr(sequence,1,4)=="Tim"
> 
> Rich
> 
> Hugh Colaco wrote:
>> Hi Statalisters,
>>
>> I am not sure how the string wildcard works. For example, I have a
>> string variable called "sequence" that has the following
>> observations:-
>>
>> 1. "Tim and Tom and Jane"
>> 2. "Tim and Jane and Tom"
>> 3. "Tom and Jane and Harry"
>> 4. "Jane and Jill"
>>
>> I want to generate a new variable that equals one if Tim is the first
>> name mentioned, and zero otherwise.
>>

*
*   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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index