BookmarkSubscribeRSS Feed
SASPhile
Quartz | Level 8
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

Thanks,
SASphile
3 REPLIES 3
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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.
deleted_user
Not applicable
Hello SASPhile,

Here is how your program could look like:

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

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";
run;

proc print data=T02_output;
run;

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?

Yoba
SASPhile
Quartz | Level 8
Thnaks yoba!
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
  • 3 replies
  • 1314 views
  • 0 likes
  • 3 in conversation