BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
gzr2mz39
Quartz | Level 8

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.

 

1 ACCEPTED SOLUTION

Accepted Solutions
s_lassen
Meteorite | Level 14

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 solution in original post

3 REPLIES 3
Shmuel
Garnet | Level 18

What output you expect of each or all of the input strings ?

gzr2mz39
Quartz | Level 8

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;

 

s_lassen
Meteorite | Level 14

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).

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1310 views
  • 0 likes
  • 3 in conversation