问题
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