BookmarkSubscribeRSS Feed
keen_sas
Quartz | Level 8

I have a string with same delimiter repeated multiple times, but i want to extract it at respective position only
Like in the example below


string is "this text = contains (values with= same delimiter "(multiple times), delimiter=")" ;

 

Required output is contains (values with= same delimiter "(multiple times), delimiter=")" ;
But the Present output is displayed as  contains (values with
This is getting truncated when the same delimiter is found again

 

Code used to test
var=scan(string,2,"=") ;
with modifier var=scan(string,2,"=","o")
still getting the same result as above

 

Is there any way by which we can control the same delimiter to ignore while scanning to get the required output.

 

6 REPLIES 6
novinosrin
Tourmaline | Level 20
data desired;
str='"this text = contains (values with= same delimiter "(multiple times), delimiter=")"' ;
want=substr(str,findc(str,'=')+1);
run;
r_behata
Barite | Level 11

The 'o' modifier improves the efficiency of the scan function by not processing the list of modifiers again, it would improve performance if the scan function is inside a loop. This does not mean that it would ignore the second occurrence of the delimiter.

 

The solution provided by @novinosrin would work in your scenario.

 

Here is an other alternative.

 

Data want;
	str='this text = contains (values with= same delimiter "(multiple times), delimiter=")';
	call scan(str,1,pos,ln,'=');
	var=substrn(str,(ln+2));
run;

 

Allaluiah
Quartz | Level 8

@r_behata  Nice , but imho not so convenient as no need to compute pos which is derivative of call scan. Also, you'd have to drop the pos,len vars after. So all in all, interesting indeed but I don't think that's a convenient/needful alternative. 

r_behata
Barite | Level 11

@Allaluiah , As I said it was just an other way of approaching the problem. Agreed, that  it may not be the most convenient in this scenario.

 

However, you also need to know that generally the call routines are efficient than the functions. Also, The call scan extends the capabilities of the regular scan function by providing dynamic position and length which may be useful to the OP if he comes across a different scenario, if it is not for you.

 

PeterClemmensen
Tourmaline | Level 20
data _null_;
string='"this text = contains (values with= same delimiter "(multiple times), delimiter=")"' ;
newstring=prxchange('s/^.*?=//', 1, string);

put string= // newstring=;
run;
s_lassen
Meteorite | Level 14

There are two possible solutions to this.

What do you want if you have a string like 'a==b=c'?

 

Do you want the result to be '=b=c' (which is not a SCAN solution, as consecutive delimiters are treated as one by SCAN), then the INDEXC solution suggested by @novinosrin is the way to go, if you want the result 'b=c' then take a look at the CALL SCAN routine, as suggested by @r_behata.

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
  • 6 replies
  • 1205 views
  • 1 like
  • 6 in conversation