DATA Step, Macro, Functions and more

Extract the word form the string

Accepted Solution Solved
Reply
Contributor
Posts: 39
Accepted Solution

Extract the word form the string

[ Edited ]

Hi Community,

 

Here I have a sample dataset, I want to extract the gram/milligram values from the string.

I tried up with compress method, but even I need more efficient step to extract in particular format.

 

data have;
input drug1$30.;
cards;
metformin 500MG
Glycopride 250mg
GLYCOMET 1GM
gLYCOMET 1GM
GLYCIPHAGE 850MG
GLYCIPHAGE 500mg
;
run;

data want;
set have;
d1 = compress(drug1,'1234567890mgMG','k');
run;

SAS Output

Obs drug1 d1123456
metformin 500MGmm500MG
Glycopride 250mgG250mg
GLYCOMET 1GMGM1GM
gLYCOMET 1GMgM1GM
GLYCIPHAGE 850MGGG850MG
GLYCIPHAGE 500mgGG500mg

 

Expected Output 

500MG

250mg

1GM

850MG

500mg

Please suggest the code which extract the output in the given format.

 

NOTE : To more simpler, Atleast let me know how to remove the characters in front of numeric value.

 

Thanks in advance!

 


Accepted Solutions
Solution
Monday
Contributor
Posts: 50

Re: Extract the word form the string

Posted in reply to Sathish_jammy

Hi,

data want(keep = unit);
set have;
unit = substr(drug1,anydigit(drug1));
run;

Regards,

Manoj

View solution in original post


All Replies
Solution
Monday
Contributor
Posts: 50

Re: Extract the word form the string

Posted in reply to Sathish_jammy

Hi,

data want(keep = unit);
set have;
unit = substr(drug1,anydigit(drug1));
run;

Regards,

Manoj

Super User
Posts: 10,557

Re: Extract the word form the string

Posted in reply to Sathish_jammy

Try

d1 = scan(drug1,-1);
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 72 views
  • 0 likes
  • 3 in conversation