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
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);
Thank you,
How about to capture the digits that precede the keywords. in the example .25
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;
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).
if there is no space between digit and unit like 0.25Mg nothing is captured
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;
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.
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.