我想搜索一个只包含某些单词而不包含其他内容的字符串变量。
数据如下:
Osteoarthritis (OA)
OA (Osteoarthritis)
OA Knee
Osteoarthritis Knee
OA + hands
Osteoarthritis, hands
OA , hip
OA
Knee OA
我可以使用
在一个输出中获得带膝盖的OA / Osteo where prxmatch("m/osteoar|oa /oi", lowcase(diagnosis)) and prxmatch("/knee/", lowcase(diagnosis)) ;
仅对于OA,我将它们放入第二个输出
where ((lowcase(diagnosis)) contains 'oa' and not prxmatch('~B|C|D|E|F|G|H|I|J|K|L|M|N|P|Q|R|S|T|U|V|W|X|Y|Z~i', upcase(diagnosis)));
但是由于数据包含OA和Oasteoarthritis,我想不出办法。
所以我想保留包含OA /骨关节炎的行,或OA / Osteo的膝盖,但摆脱其他一切。 膝关节OA / Osteo容易保持,但我很难保持那些只有OA /骨关节炎的人。 变量很长,很乱,没有常规模式,所以不可能列出我想删除的那些。
答案 0 :(得分:0)
字符串比较与SAS相当笨拙。一种方法是使用索引函数。这将查找是否找到匹配的字符串模式给定的变量。请注意,虽然SAS通常不区分大小写,但字符串比较不是。 (因此,中间的upcase datastep。
工作样本:
data begin; /*Silly way, but works.*/
words='Osteoarthritis (OA)'; output;
words='OA (Osteoarthritis)'; output;
words='OA Knee'; output;
words='Osteoarthritis Knee';output;
words='OA + hands'; output;
words='Osteoarthritis, hands'; output;
words='OA , hip'; output;
words='OA'; output;
words='Knee OA'; output;
run;
data begin; /*This is done purely to ease the comparison below*/
set begin;
words2=upcase(words);
run;
data Only_wanted;
set begin;
if index(words2, 'OA')>0 or /*select OA */
index(words2, 'OSTEOARTHRITIS')>0 or /*or OSTEOARTHRITIS */
(
index(words2, 'OA')>0 or
index(words2, 'OSTEO')>0 AND /*<=! AND here */
index(words2, 'KNEE')>0
);
run;
在第一个和第二个条件之间存在一点冗余,但这应该给你相当多的工作量。 有关索引的更多信息,请参阅:http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000212242.htm
答案 1 :(得分:0)
我不确定您的实际任务有多复杂,但根据数据和既定目标,这可能对您有用:
var html = @"
<div>
<strong>First name</strong><em>italic</em>: Fake<br>
<strong>Bold</strong> <a href='#'>hyperlink</a><br>.
<strong>bold</strong>
<strong>bold</strong> <br>
text
</div>
<div>
<strong>Title</strong>: Mr<BR>
<strong>First name</strong>: Fake<br>
<strong>Surname</strong>: Guy<br>
</div>";
var document = new HtmlDocument();
document.LoadHtml(html);
// 1. <strong>
var strong = document.DocumentNode.SelectNodes("//strong");
if (strong != null)
{
foreach (var node in strong.Where(
// 2. followed by non-empty text node
x => x.NextSibling is HtmlTextNode
&& !string.IsNullOrEmpty(x.NextSibling.InnerText.Trim())
// 3. followed by <br>
&& x.NextSibling.NextSibling is HtmlNode
&& x.NextSibling.NextSibling.Name.ToLower() == "br"))
{
Console.WriteLine("{0} {1}", node.InnerText, node.NextSibling.InnerText);
}
}
对于每条记录,遍历每个单词。如果data have;
input diagnosis $char50.;
datalines;
Osteoarthritis (OA)
OA (Osteoarthritis)
OA Knee
Osteoarthritis Knee
OA + hands
Osteoarthritis, hands
OA , hip
OA
Knee OA
;
run;
data want;
set have;
do i=1 to countw(diagnosis);
if upcase(scan(diagnosis,i)) not in('OSTEOARTHRITIS'
,'(OA)'
,'OA'
,'KNEE') then delete;
end;
drop i;
run;
中的单词不符合我们的利益,则将其删除。