Bookmark and Share

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


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

Re: st: macro problem


From   Jeph Herrin <[email protected]>
To   [email protected]
Subject   Re: st: macro problem
Date   Mon, 11 Jun 2012 15:04:54 -0400

Thanks. In both cases I should have clarified that I want the list of match strings to be part of a macro (as in my example) because the list will change from time to time (and I prefer to isolate code changes to macros). In that case, your example and others produce surprising results:

 . local list "a b" "b c" "c d"

 . foreach s in `list' {
   2.    di "`s'"
   3.    }
 a
 b
 b c
 c d

. local list "a" "b"

. foreach s in `list' {
  2.    di `"`s'"'
  3.    }
 a"
 b

(note the trailing " on the -a-) and so on.

cheers,
Jeph





On 6/11/2012 12:29 PM, Nick Cox wrote:
" " have two roles in Stata; they act as string delimiters (which by default are stripped) and they act as literal characters which you want to preserve as part of a string (in which you often need `" "' as delimiters).

However, this works

. foreach s in "a b" "b c" "c d" {
   2. di "`s'"
   3. }
a b
b c
c d

so maybe something simpler would also work for you.

Nick
[email protected]

Jeph Herrin

There is no problem with -strpos-, which never returns missing and which
is zero exactly when I want it to be. The problem was in creating the
original macro and then referencing the elements so that they appear in
-strpos- with the correct number of quotations marks; I was achieving
either too many or too few of the latter.

The solution (or at least, a solution) is for local -matchlist- needed
to be enclosed in compound quotes, and then ditto the reference:

     local matchlist `""string 1" "string 2" "string 3" .... "string 55""'

     gen byte match=0
     foreach S of local matchlist {
          replace match = 1 if strpos(strvar,`"`S'"')
     }


The reference in -strpos- looks wrong to me - I would anticipate that
`S' would include the necessary "'s for -strpos-, but it does not.
Without the extra quotes on the original local, however, `"`S'"'
resolves to include too many "'s.

On 6/11/2012 11:58 AM, Richard Goldstein wrote:

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
"S" is not found; so, in addition to the quotes, etc., I guess you want
to ensure that strpos is>0 (and maybe<.)

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?

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


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index