DATA Step, Macro, Functions and more

how to extract the valuew which are in parentheses

Accepted Solution Solved
Reply
Contributor
Posts: 62
Accepted Solution

how to extract the valuew which are in parentheses

Hello,

I have a string of characters and I need those values in parentheses. 

data title;
input title $20.;
datalines;
National Geography (NG)
Time (T)
National Food and Energy Council (NFC)
Food and Drug Administration (FDA)
National Hockey League (NHL)
National Football League (NFL)
;
run;

The results I want are: 

NG

T

NFC

NHL

NFL

Thanks for the help!


Accepted Solutions
Solution
3 weeks ago
Super User
Posts: 5,509

Re: how to extract the valuew which are in parentheses

This takes the first set of characters in parentheses, and doesn't consider whether there might be other sets of parentheses:

 

newvar = scan(oldvar, 2, '()');

View solution in original post


All Replies
Regular Contributor
Posts: 191

Re: how to extract the valuew which are in parentheses

If they always are at the end this is a quicky:

 

newTitle = compress(reverse(scan(reverse(title),1)),'()');

 

Untested, but should work Smiley Happy

//Fredrik

Contributor
Posts: 62

Re: how to extract the valuew which are in parentheses

Thanks for reply. It works !

Solution
3 weeks ago
Super User
Posts: 5,509

Re: how to extract the valuew which are in parentheses

This takes the first set of characters in parentheses, and doesn't consider whether there might be other sets of parentheses:

 

newvar = scan(oldvar, 2, '()');

Contributor
Posts: 62

Re: how to extract the valuew which are in parentheses

Posted in reply to Astounding

Thanks for that! It works!

Super User
Posts: 10,028

Re: how to extract the valuew which are in parentheses

data title;
input title $40.;
if findc(title,')','b')=length(title) then want=scan(title,-1,'() ') ;
datalines;
National Geography (NG)
Time (T)
National Food and Energy Council (NFC)
Food and Drug Administration (FDA)
National Hockey League (NHL)
National Football League (NFL)
;
run;

☑ This topic is solved.

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

Discussion stats
  • 5 replies
  • 92 views
  • 0 likes
  • 4 in conversation