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!

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 3 replies
  • 631 views
  • 0 likes
  • 3 in conversation