Here is a solution: use PRX to find the whole level string, use COMPRESS with modifiers 'kd' (keep digits) to extract the digits:
data have;
input;
text_string=_infile_;
cards;
afd ed 44 1) Level space 1,2,3 (e.g. Level 1) 3eyrt4drf Level 2 Level (1)
fdafdsfdsfsfsd 2) Level hyphen space 1,2,3 (e.g. Level- 2) fdsafafa
3) Level space hyphen space 1,2,3 (e.g. Level - 3)
4) Level hyphen 1,2,3 (e.g. Level-1)
5) Level space parenthesis around 1,2,3 (e.g. Level (2))
;run;
data want;
_prxid=prxparse('/Level \d+|Level ?- ?\d+|Level \(\d+\)/');
set have;
_start = 1;
_stop = length(text_string);
length incident_level $200;
call prxnext(_prxid, _start, _stop, trim(text_string), _pos, _len);
do while (_pos > 0);
call catx(' ',incident_level,compress(substr(text_string, _pos, _len),,'kd'));
call prxnext(_prxid, _start, _stop, trim(text_string), _pos, _len);
end;
keep text_string incident_level;
run;
Of course, you may want to use the "I" modifier for you regular expression, if you have stuff like "LEVEL 1" or "level -1".
I changed the CATX function to CALL CATX, as that is more efficient (concatenates in place instead of copying the old string to temporary storage, concatenating there, and then copying the result back).
... View more