DATA Step, Macro, Functions and more

Match and Value

Reply
Super Contributor
Posts: 702

Match and Value


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

Super Contributor
Posts: 448

Re: Match and Value

[ Edited ]

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
Super Contributor
Posts: 702

Re: Match and Value

Posted in reply to SuryaKiran

Thank you,

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

Super Contributor
Posts: 448

Re: Match and Value

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
PROC Star
Posts: 226

Re: Match and Value

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

Super Contributor
Posts: 702

Re: Match and Value

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

Super User
Posts: 10,610

Re: Match and Value

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;
Ask a Question
Discussion stats
  • 6 replies
  • 93 views
  • 2 likes
  • 4 in conversation