DATA Step, Macro, Functions and more

extract string that varies in length and number of delimiters

Accepted Solution Solved
Reply
Regular Contributor
Posts: 181
Accepted Solution

extract string that varies in length and number of delimiters

Hi all,

 

If I have values that consistently are in this type of layout- (that is  a wanted character string between ( x) and a (char date.)..

 

AE: (6) urinary tract infection 22-NOV-2017

 

Is there a way to  just extract the 'urinary tract infection' to a new variable?

 

Help is appreciated!

 


Accepted Solutions
Solution
‎05-08-2018 03:54 PM
PROC Star
Posts: 1,604

Re: extract string that varies in length and number of delimiters

[ Edited ]
data have;
input var $80.;
call scan(var,-1,position, length,' ');
k1=index(var,')');
need= substr(var,index(var,')')+1,position-k1-1);
drop k: position length;
cards;
AE: (6) urinary tract infection 22-NOV-2017
;

 

View solution in original post


All Replies
Solution
‎05-08-2018 03:54 PM
PROC Star
Posts: 1,604

Re: extract string that varies in length and number of delimiters

[ Edited ]
data have;
input var $80.;
call scan(var,-1,position, length,' ');
k1=index(var,')');
need= substr(var,index(var,')')+1,position-k1-1);
drop k: position length;
cards;
AE: (6) urinary tract infection 22-NOV-2017
;

 

Regular Contributor
Posts: 181

Re: extract string that varies in length and number of delimiters

Posted in reply to novinosrin

@novinosrin  thank you!  HUGE help!!!

Contributor
Posts: 70

Re: extract string that varies in length and number of delimiters

@jenim514  You could mark the question as answered and close the thread

Valued Guide
Posts: 560

Re: extract string that varies in length and number of delimiters

If you data looks like the example you showed i.e. you have lowercase for the string you want to extract remaning other than lowercase then you can use COMPRESS() function with "k" (keeps the characters in the list instead of removing them) and "l" modifier (adding lowercase to list).

DATA HAVE;
STRING="AE: (6) urinary tract infection 22-NOV-2017";
Want_Str=STRIP(COMPRESS(STRING,"","kl"));
run;
Thanks,
Suryakiran
☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 148 views
  • 2 likes
  • 4 in conversation