1

Suppose you have the macro

global LabNames "3M"  "ABBOTT"  "MERCK SHARP DOHME"

I am using the quotes so that the words are correctly grouped (MERCK SHARP DOHME is one company, not three different ones). I am trying to write a program that goes over a variable and replaces it when it has one of the strings of LabNames as a substring.

Let us start with the part of the code that works fine.

foreach company of global LabNames {
     display "`company'"
}

This code proceeds as needed in my case - lists 3 different companies (not 5). The following code, however, does not run correctly. It breaks down for 3M.

gen hasLab = 0

foreach company of global LabNames {
     display "`company'"
     replace hasLab = (index(lab,`"`company'"'))
     replace lab = `"`company'"' if hasLab > 0 
}

If we apply this code to

    lab
    asdf 3M
    3M
    ABBOTT
    ABBOTT asdf
    MERCK SHARP DOHME AS
    MERCK SHARP DOHME 4

we get

    lab
    3M
    asdf 3M
    ABBOTT
    ABBOTT
    MERCK SHARP DOHME
    MERCK SHARP DOHME

Would you know what to do so that the code can handle the 3M case correctly?

Nick Cox
  • 35,529
  • 6
  • 31
  • 47
M. Otts
  • 11
  • 2
  • I changed the order of the global macro to global LabNames "ABBOTT" "3M" "MERCK SHARP DOHME" and now it seems to work. An oddity? Anyone knows what is going on? – M. Otts Jun 27 '12 at 18:27

1 Answers1

1

You have unnecessary quotes in your global. It's getting messed up. See

. global LabNames "3M" "ABBOTT" "MERCK SHARP DOHME"
. mac list LabNames
LabNames:       3M" "ABBOTT" "MERCK SHARP DOHME

You can just type

global LabNames 3M ABBOTT "MERCK SHARP DOHME" 

See help macrolists for some tips.

Keith
  • 1,037
  • 6
  • 13