Matching variable strings

假装没事ソ 提交于 2019-12-24 07:52:48

问题


I have a variable that looks like this:

045672, 19274
061483, 21124
068346, 32948

And another that looks as follows:

[045672, 19274; 056843, 20483] AAA8793307546; [061483, 21124] AZS69482148
[045672, 19274; 056843, 20483] AAA8793307546; [061483, 21124] AZS69482148
[068346, 32948] BGJ569788313

How can I keep the part of the second variable that matches the first?


回答1:


The following works for me:

clear
input str15 v1 str75 v2
"045672, 19274" "[045672, 19274; 056843, 20483] AAA8793307546; [061483, 21124] AZS69482148"
"061483, 21124" "[045672, 19274; 056843, 20483] AAA8793307546; [061483, 21124] AZS69482148"
"068346, 32948" "[068346, 32948] BGJ569788313" 
end

split v2, parse("[")
drop v2

reshape long v2, i(v1) j(id)
keep if strpos(v2, v1)
drop id

list

     +---------------------------------------------------------------+
     |            v1                                              v2 |
     |---------------------------------------------------------------|
  1. | 045672, 19274   045672, 19274; 056843, 20483] AAA8793307546;  |
  2. | 061483, 21124                      061483, 21124] AZS69482148 |
  3. | 068346, 32948                     068346, 32948] BGJ569788313 |
     +---------------------------------------------------------------+

What this code snippet does, is split the second variable in two parts and then reshape the data to find the pairs. After that, it simply keeps the matching observations.



来源:https://stackoverflow.com/questions/57347018/matching-variable-strings

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!