☑ This topic is solved.
Need further help from the community? Please
sign in and ask a new question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Posted 01-07-2025 03:30 AM
(589 views)
data sorted_string;
length Str $50 ;
Str = "ASDFKBSDEFWKSR";
run;
Sorting last N letters in str variable
1 ACCEPTED SOLUTION
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Here is an easy to follow approach
data want;
set sorted_string;
array a {100} $ _temporary_;
N = 5;
pos = max(1, length(str)-(N - 1));
sub = substrn(str, pos, N);
do i = 1 to length(sub);
a[i] = char(sub, i);
end;
call sortc(of a[*]);
substr(str, max(1, pos, N)) = cats(of a[*]);
run;
5 REPLIES 5
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
How long can str be and how large can N be?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
HI PeterClemmensen,
suppose last 5 letters
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Here is an easy to follow approach
data want;
set sorted_string;
array a {100} $ _temporary_;
N = 5;
pos = max(1, length(str)-(N - 1));
sub = substrn(str, pos, N);
do i = 1 to length(sub);
a[i] = char(sub, i);
end;
call sortc(of a[*]);
substr(str, max(1, pos, N)) = cats(of a[*]);
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Here's one way for N=5
data intermediate;
set sorted_string;
array letter $ letter1-letter5 _temporary_;
last5letters=substr(str,length(str)-4);
do i=1 to 5;
letter(i)=substr(last5letters,i,1);
end;
call sortc(of letter:);
last5letters_sorted=cats(of letter:);
sorted_string=cats(substr(str,1,length(str)-5),last5letters_sorted);
drop i;
run;
For general N, you could replace 5 with a macro variable.
--
Paige Miller
Paige Miller
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi PaigeMiller
Thank you very much your solution