DATA Step, Macro, Functions and more

regular expressions

Super Contributor
Posts: 647

regular expressions

I'm not that good at reg exp. I tried my luck ..was not sucessful. Need your help on this.

I have a filed called "Drug_name". It will have the list of drugs. example: Paracetmol 4.8MG VIAL

The task is to extract the digits from it. in this case its 4.8.

Possible scenarios:
the number of digits vary between 1 and 4
so we need to have an or condition for number of digits d d\d d\d\d d\d\d\d

and we should consider decimal also between two digits as in the example.

the values should go to the filed called strength,.

in this case strength is 4.8

Super Contributor
Super Contributor
Posts: 3,174

Re: regular expressions

Use ANYALPHA to test if non-numeric present, then use COMPRESS function to remove chars A-Z after UPCASE, in a DATA step to assign a SAS variable, then use INPUT (convert to SAS NUMERIC) and PUT function (to convert back to a "formatted" CHARACTER type variable, displayed as a number).

Scott Barry
SBBWorks, Inc.
Posts: 0

Re: regular expressions

Hello SASPhile,

Here is how your program could look like:

data T01_input;
length drug $50;
infile cards delimiter=',';
input id drug;
1,Paracetmol 4.8MG VIA
2,Paracetmol 423.23422238MG VIA
3,Paracetmol 42342.38MG VIA
4,Paracetmol x.8MG VIA

data T02_output;
set T01_input;
length myDigits $50;
if prxmatch('/.* \d{1,4}\.\d+MG .*/',drug)
then myDigits = prxchange('s/.* (\d{1,4}\.\d+)MG .*/$1/', -1, drug);
else myDigits = "NOT FOUND";

proc print data=T02_output;

You would get the following output:

1 Paracetmol 4.8MG VIA 1 4.8
2 Paracetmol 423.23422238MG VIA 2 423.23422238
3 Paracetmol 42342.38MG VIA 3 NOT FOUND
4 Paracetmol x.8MG VIA 4 NOT FOUND

If you need to convert the myDigit string to number ... use the input function.

Does it help?

Super Contributor
Posts: 647

Re: regular expressions

Thnaks yoba!
Ask a Question
Discussion stats
  • 3 replies
  • 3 in conversation