Extract part of a string having positional delimiter

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 120
Accepted Solution

Extract part of a string having positional delimiter

Hi there,

 

I am trying to extract part of a string available between 3rd and 4th pipe. Can anybody please assist me to move forward. 

data HAVE;
length id $3 comment $50 ;
input id comment $ ;
datalines;
101 ABC|X01|X02|2001|DEF
102 ABC|X001|X002|2002|DEF
103 ABC|X0001|X0002|2003|DEF
104 ABC|X00001|X00002|2004|DEF
105 ABC|X01|X02|2005|DEF
;
RUN;


data WANT;
length id $3 comment $50 ;
input id comment $ ;
datalines;
101 2001
102 2002
103 2003
104 2004
105 2005
;
RUN;

Thank you in advance for your kind guidance. 

Regards,

Swain

Accepted Solutions
Solution
‎03-09-2018 09:35 AM
Super User
Super User
Posts: 8,126

Re: Extract part of a string having positional delimiter

Posted in reply to DeepakSwain

You could use the SCAN() function.

comment=scan(comment,4,'|');

View solution in original post


All Replies
Super User
Posts: 10,787

Re: Extract part of a string having positional delimiter

Posted in reply to DeepakSwain
data HAVE;
length id $3 comment $50 ;
input id comment $ ;

want=scan(comment,-2,'|');

datalines;
101 ABC|X01|X02|2001|DEF
102 ABC|X001|X002|2002|DEF
103 ABC|X0001|X0002|2003|DEF
104 ABC|X00001|X00002|2004|DEF
105 ABC|X01|X02|2005|DEF
;
RUN;
Frequent Contributor
Posts: 120

Re: Extract part of a string having positional delimiter

Hi Ksharp, 

can you kindly help me to extend this code to count the position of PIPE from ABC. As ABC segment may be preceded by other string. My target is to extract content available between 3rd and 4th pipe from the term ABC. 

data HAVE;
length id $3 comment $50 ;
input id comment $ ;
datalines;
101 X01|X02|ABC|X01|X02|2001|DEF
102 X01|X02|X02|ABC|X001|X002|2002|DEF
103 X01|X02|ABC|X0001|X0002|2003|DEF
104 X01|X02|X02|ABC|X00001|X00002|2004|DEF
105 X01|X02|ABC|X01|X02|2005|DEF
;
RUN;


data WANT;
length id $3 comment $50 ;
input id comment $ ;
datalines;
101 2001
102 2002
103 2003
104 2004
105 2005
;
RUN;

Once again thanks for your kind response. 

 

Swain
Solution
‎03-09-2018 09:35 AM
Super User
Super User
Posts: 8,126

Re: Extract part of a string having positional delimiter

Posted in reply to DeepakSwain

You could use the SCAN() function.

comment=scan(comment,4,'|');
☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 165 views
  • 6 likes
  • 3 in conversation