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,'|');

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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