BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
DeepakSwain
Pyrite | Level 9

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
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

You could use the SCAN() function.

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

View solution in original post

3 REPLIES 3
Ksharp
Super User
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;
DeepakSwain
Pyrite | Level 9

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
Tom
Super User Tom
Super User

You could use the SCAN() function.

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

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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