2016-08-10 9 views
0

私はその中のどこかに大文字の単語を含む文字列を持っています。私はSASを使って、その1つの単語を新しい変数に抽出したいと思います。SASは文字列内で大文字の単語を見つける

私は、大文字で始まる文頭に2つ以上の大文字を含む単語を見つけることをコードする方法を見つける必要があると思います。

つまり、私は変数「言葉」を作成するにはどうすればよい:

data example; 

    length txtString $50; 

    length word $20; 

    infile datalines dlm=','; 

    input txtString $ word $; 

datalines; 

This is one EXAMPLE. Of what I need.,EXAMPLE 

THIS is another.,THIS 

etc ETC,ETC 

; 

run; 

ホープ誰かが助けることができるが、問題は、事前に

おかげで明らかである

+0

ここprxsubstr()関数呼び出しを使用して試料溶液ですバイト関数を使用して大文字のascii値をチェックする – DCR

答えて

0

はと交換/正規表現マッチを考えてみましょう2つのタイプの一致を含むようにネガティブな見解を追加する:

  1. 連続する大文字の単語とそれに続くsp (文の先頭にタイトルケースを避けるために):最低2文字とピリオド(([A-Z ]){2,})
  2. 連続した大文字の言葉:最低2つの文字とエースは、(文の先頭にタイトルケースを避けるため)(([A-Z.]){2,})

警告:この解決策は、Iという記事も一致します。これは技術的にはすべて大文字の1語であるため、有効な一致です。英語の唯一のタイプであるため、このような特別な場合にはtranwrd()を置き換えてください。実際、関連して、この解はすべての大文字の単語に一致します。

data example; 
    length txtString $50; 
    length word $20; 
    infile datalines dlm=','; 
    input txtString $ word $; 
datalines; 
This is one EXAMPLE. Of what I need.,EXAMPLE 
THIS is another.,THIS 
etc ETC,ETC 
; 
run; 

data example; 
    set example; 
    pattern_num = prxparse("s/(?!(([A-Z ]){2,})|(([A-Z.]){2,})).//"); 
    wordextract = prxchange(pattern_num, -1, txtString); 

    wordextract = tranwrd(wordextract, " I ", ""); 
    drop pattern_num; 
run; 

txtString        word  wordextract 
This is one EXAMPLE. Of what I need. EXAMPLE EXAMPLE 
THIS is another.      THIS  THIS 
etc ETC         ETC  ETC 
0

SASは、指定された文字列内の指定された正規表現パターンに一致するサブストリングの開始位置と長さを見つけprxsubstr()関数呼び出しを有しています。

data solution; 
    set example; 

    /* Build a regex pattern of the word to search for, and hang on to it */ 
    /* (The regex below means: word boundary, then two or more capital letters, 
    then word boundary. Word boundary here means the start or the end of a string 
    of letters, digits and/or underscores.) */ 
    if _N_ = 1 then pattern_num = prxparse("/\b[A-Z]{2,}\b/"); 
    retain pattern_num; 

    /* Get the starting position and the length of the word to extract */ 
    call prxsubstr(pattern_num, txtString, mypos, mylength); 

    /* If a word matching the regex pattern is found, extract it */ 
    if mypos ^= 0 then word = substr(txtString, mypos, mylength); 
run; 

SASのprxsubstr()のドキュメント:http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a002295971.htm

正規表現の単語境界情報:文字列の長さのためにforループ設定http://www.regular-expressions.info/wordboundaries.html

関連する問題