BookmarkSubscribeRSS Feed
SASPhile
Quartz | Level 8


Text
.25 Mg Every 3 Hours Prn

MATCH=prxmatch('m/Meq|mg|ml|gram(\w)*|gm|mcg/i',text);

The result of match is 5. But I would like to capture Mg as value  in a new column as below


Text                                           Match          Value                
.25 Mg Every 3 Hours Prn              5               Mg

6 REPLIES 6
SuryaKiran
Meteorite | Level 14

I'm using SCAN and SUBSTR, but it may change depending on how you data flows. PRXMATCH gives you the position and you can use that values in SUBSTR.

 

MATCH=SCAN(SUBSTR(TEXT,prxmatch('m/Meq|mg|ml|gram(\w)*|gm|mcg/i',text)),1);

 

Thanks,
Suryakiran
SASPhile
Quartz | Level 8

Thank you,

How about to capture the digits that precede the keywords. in the example  .25

SuryaKiran
Meteorite | Level 14

Something like this would work, also if you want to convert into numeric use INPUT function. I'm assuming value is separated from units with a space here, if not all the time in your data then change accordingly. 

Data have;
Text ="Example text .25 Mg Every 3 Hours Prn";
MATCH=SCAN(SUBSTR(TEXT,prxmatch('m/Meq|mg|ml|gram(\w)*|gm|mcg/i',text)),1);
value=SCAN(SUBSTR(TEXT,1,prxmatch('m/Meq|mg|ml|gram(\w)*|gm|mcg/i',text)-2),-1,' ');
run;
Thanks,
Suryakiran
s_lassen
Meteorite | Level 14

I understand that you want both the number and the unit. The something like this may work:

data _null_;
  text='.25 Mg Every 3 Hours Prn';
  prxid=prxparse('/([0-9\.]+)\s+(Meq|mg|ml|gram(\w)*|gm|mcg)/i');
  if prxmatch(prxid,text) then do;
    number=prxposn(prxid,1,text);
    unit=prxposn(prxid,2,text);
    end;
  put _all_;
run;

The PRXPOSN function returns the string inside a capture buffer (the stuff inside the parantheses).

SASPhile
Quartz | Level 8
if the number is 0.25, the decimal is not captured. shows as 25
if there numbers are like .63-0.125 mg,  .63-0.125 are not captured.,shows as 125

if there is no space between digit and unit like 0.25Mg nothing is captured

Ksharp
Super User
Data have;
Text ="Example text .25 Mg Every 3 Hours Prn";
pid=prxparse('m/Meq|mg|ml|gram(\w)*|gm|mcg/i');
call prxsubstr(pid,text,p,l);
if p then want=substr(text,p,l);
run;
proc print;run;

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
  • 6 replies
  • 1630 views
  • 2 likes
  • 4 in conversation