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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 697 views
  • 2 likes
  • 4 in conversation