Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# RE: st: macro problem

 From Nick Cox To "'statalist@hsphsun2.harvard.edu'" Subject RE: st: macro problem Date Mon, 11 Jun 2012 17:20:41 +0100

```I agree with Richard's first point.

His second point doesn't worry me. -if strpos(<arguments>)- is exactly equivalent to -if strpos(<arguments>) > 0-. Also, I don't think -strpos()- ever returns numeric missing.

Here is a simple example that works:

. l, sep(0)

+-----------+
|      var1 |
|-----------|
1. |      frog |
3. |      newt |
4. |    newton |
6. |    froggy |
+-----------+

. gen match = 0

. foreach txt in "frog" "toad" "newt" {
2. replace match = 1 if strpos(var1, "`txt'")
3. }

. l, sep(0)

+-------------------+
|      var1   match |
|-------------------|
1. |      frog       1 |
3. |      newt       1 |
4. |    newton       1 |
5. | toad hall       1 |
6. |    froggy       1 |
+-------------------+

Nick
n.j.cox@durham.ac.uk

-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Richard Goldstein
Sent: 11 June 2012 16:59
To: statalist@hsphsun2.harvard.edu
Cc: Jeph Herrin
Subject: Re: st: macro problem

you don't make it clear about what doesn't work

but here is a guess: you are getting match=1 in all cases (or at least
all where strvar is not missing); remember, however, that strpos=0 if
to ensure that strpos is >0 (and maybe <.)

Rich

On 6/11/12 11:50 AM, Jeph Herrin wrote:
> This should be trivial, but for some reason I cannot get it.
>
> I have a string variable, and I have a list of string values I need to
> match against it. There are occasional trailing or leading characters in
> the variable, so I am using -strpos- to find matches. Thus
>
>   local matchlist "string 1" "string 2"  "string 3" .... "string 55"
>
>   gen byte match=0
>   foreach S in `matchlist' {
>         replace match = 1 if strpos(strvar,`S')
>   }
>
> Now, I know I have got the quotes and macro evaluations wrong, but that
> is the problem: I've tried this many different ways, and yet cannot seem
> to find a combination that works. Any thoughts?
>
> thanks,
> Jeph
*
*   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/

*
*   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/
```