Does anyone know how to setup one prxparse expression to capture the following 5 pieces of text? What comes before or after the 5 pieces of text shown below can be anything.
1) Level space 1,2,3 (e.g. Level 1)
2) Level hyphen space 1,2,3 (e.g. Level- 2)
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))
Thank you.
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).
What output you expect of each or all of the input strings ?
I'm searching the variable text_string for these 5 patterns.
The regular expression I'm asking about will go inside prxparse.
_prxid will be used in prxnext.
I want to create the variable incident_level.
_prxid = prxparse('regular expression');
_start = 1;
_stop = length(text_string);
call prxnext(_prxid, _start, _stop, trim(text_string), _pos, _len);
do while (_pos > 0);
incident_level = catx(' ',incident_level,substr(text_string, _pos, _len));
call prxnext(_prxid, _start, _stop, trim(text_string), _pos, _len);
end;
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).
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.