Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • matching one variable with another variable?

    Dear All, I found this question here (https://bbs.pinggu.org/thread-7419648-1-1.html). The data is
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str14 address str1 name int test
    "A company"  "A" 1
    "B company"  "B" 1
    "C company"  "C" 1
    "D company"  ""  0
    "G company"  ""  0
    "BD company" ""  1
    "E company"  ""  0
    "AF company" ""  1
    end
    Given `address' and `name', I'd like to define a dummy `test' which is equal to 1 if the company name in `address' matches any one of elements of `name', 0 otherwise. Any suggestions are highly appreciated.
    Ho-Chuan (River) Huang
    Stata 17.0, MP(4)

  • #2
    Another related question is here (https://bbs.pinggu.org/forum.php?mod...=1#pid63199808). The data is
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str24 address str7 keywords int YesorNo str7 whichword
    "United States of America" "America" 1 "America"
    "shanxi province china"    "china"   1 "china"  
    "shanxi province china"    ""        1 "china"  
    "United States of America" ""        1 "America"
    "anhui province china"     ""        1 "china"  
    "United States of America" ""        1 "America"
    "shanxi province china"    ""        1 "china"  
    "United States of America" ""        1 "America"
    "United Kingdom(UK)"       ""        0 "NULL"   
    "United Kingdom(UK)"       ""        0 "NULL"   
    "east of Canada"           ""        0 "NULL"   
    "Canada west"              ""        0 "NULL"   
    end
    if "address" contain any one in the list of "keywords", the "YesorNo" is defined as 1, and "whichword" reports the the word in the list of "keywords". Otherwise, the "YesorNo" reports 0,and "whichword" reports "NULL". Any suggestions?
    Ho-Chuan (River) Huang
    Stata 17.0, MP(4)

    Comment


    • #3
      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input str14 address str1 name int test
      "A company"  "A" 1
      "B company"  "B" 1
      "C company"  "C" 1
      "D company"  ""  0
      "G company"  ""  0
      "BD company" ""  1
      "E company"  ""  0
      "AF company" ""  1
      end
      
      levelsof name, local(names) sep(|) clean
      gen wanted = regexm(address, "(`names')")
      Res.:

      Code:
      . l, sep(10)
      
           +-----------------------------------+
           |    address   name   test   wanted |
           |-----------------------------------|
        1. |  A company      A      1        1 |
        2. |  B company      B      1        1 |
        3. |  C company      C      1        1 |
        4. |  D company             0        0 |
        5. |  G company             0        0 |
        6. | BD company             1        1 |
        7. |  E company             0        0 |
        8. | AF company             1        1 |
           +-----------------------------------+

      #2

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input str24 address str7 keywords int YesorNo str7 whichword
      "United States of America" "America" 1 "America"
      "shanxi province china"    "china"   1 "china"  
      "shanxi province china"    ""        1 "china"  
      "United States of America" ""        1 "America"
      "anhui province china"     ""        1 "china"  
      "United States of America" ""        1 "America"
      "shanxi province china"    ""        1 "china"  
      "United States of America" ""        1 "America"
      "United Kingdom(UK)"       ""        0 "NULL"   
      "United Kingdom(UK)"       ""        0 "NULL"   
      "east of Canada"           ""        0 "NULL"   
      "Canada west"              ""        0 "NULL"   
      end
      
      levelsof keywords, local(names) sep(|) clean
      gen wanted= regexs(0) if regexm(address, "(`names')")
      replace wanted= "NULL" if wanted==""
      Res.:

      Code:
      . l, sep(12)
      
           +--------------------------------------------------------------------+
           |                  address   keywords   YesorNo   whichw~d    wanted |
           |--------------------------------------------------------------------|
        1. | United States of America    America         1    America   America |
        2. |    shanxi province china      china         1      china     china |
        3. |    shanxi province china                    1      china     china |
        4. | United States of America                    1    America   America |
        5. |     anhui province china                    1      china     china |
        6. | United States of America                    1    America   America |
        7. |    shanxi province china                    1      china     china |
        8. | United States of America                    1    America   America |
        9. |       United Kingdom(UK)                    0       NULL      NULL |
       10. |       United Kingdom(UK)                    0       NULL      NULL |
       11. |           east of Canada                    0       NULL      NULL |
       12. |              Canada west                    0       NULL      NULL |
           +--------------------------------------------------------------------+
      Last edited by Andrew Musau; 12 Nov 2019, 03:08.

      Comment


      • #4
        Dear Andrew, I cannot appreciate more for your excellent solutions.
        Ho-Chuan (River) Huang
        Stata 17.0, MP(4)

        Comment

        Working...
        X