- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I want to extract the substring from a string till the last occurrence of my delimiter, for example a dataset has values a below..
str
A
A,B
A,B,C
A,B,C,D
I want to extract the substring till the last occurence of ',' in the above dataset.the resultant dataset should look like ...
str
A
A
A,B
A,B,C
Thanks in advance..
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Just another variation:
data input;
input str $;
datalines;
A
A,B
A,B,C
A,B,C,D
;
run;
data want;
set input;
SubStrLength=findc(str, ',',-length(str)) ;
if SubStrLength=0 then WantStr=Str;
else WantStr=substr(str,1,SubStrLength-1);
drop SubStrLength;
run;
proc print;
run;
And this from @cidab:
data input; input str $; want = substr(str,1,length(str)-indexc(reverse(trim(str)),',')); ...
**trim the input string, reverse it and find the position of last (now first) comma, then substring input string from start to string length minus the number of characters to drop;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
[pre]
data input;
input str $;
datalines;
A
A,B
A,B,C
A,B,C,D
;
run;
data extract1(drop=_str_length _i _comma_pos);
set input;
_str_length = length(str);
do _i = 1 to _str_length;
if substr(str,_i,1) = ',' then _comma_pos=_i-1;
end;
ext=substr(str,1,max(_comma_pos,1));
output;
run;
data extract2(drop=_last_element);
set input;
_last_element=scan(str,-1);
ext=tranwrd(str,cats(',',_last_element),'');
run;
[/pre]
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
result= scan(str,-1);
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
scan with -1 will give last word but I want remaining words excepting last word.... Message was edited by: kishore415
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Just another variation:
data input;
input str $;
datalines;
A
A,B
A,B,C
A,B,C,D
;
run;
data want;
set input;
SubStrLength=findc(str, ',',-length(str)) ;
if SubStrLength=0 then WantStr=Str;
else WantStr=substr(str,1,SubStrLength-1);
drop SubStrLength;
run;
proc print;
run;
And this from @cidab:
data input; input str $; want = substr(str,1,length(str)-indexc(reverse(trim(str)),',')); ...
**trim the input string, reverse it and find the position of last (now first) comma, then substring input string from start to string length minus the number of characters to drop;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Your code looks simple and works..thanks a lot...
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
data input;
input str $20.;
position=findc(str,',','b');
if position=0 then want=str;
else want=substr(str,1,position-1);
datalines;
A
A,Brertyr
A,B,Ce
A,B,C,Ddsd
;
run;
[/pre]
Ksharp
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
data input;
input str $;
want = substr(str,1,length(str)-indexc(reverse(trim(str)),','));
**trim the input string, reverse it and find the position of last (now first) comma, then substring input string from start to string length minus the number of characters to drop;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
cidab has a great solution. I've used this a couple times now to only extract what I need from a string.
Nathan Och
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
data input_extract;
set input;
len = length(str);
do i =1 to len;
len1 = len -2;
sub_str = substr(str,1,len1);
end;
run;