🔒 This topic is solved and locked.
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 04-30-2018 04:00 AM
(2443 views)
Hi all I am new to sas.
I hope you can help.
I am trying to break up a strings to receive the last group of digits contained within the string.
Eg1 my string
"#113#468#9999#2474#.#.#.#.#"
Result
2474
Eg2
"5743#788#1#9#89999#64#68#.#.#"
Result
68
Thanks in advance.
I hope you can help.
I am trying to break up a strings to receive the last group of digits contained within the string.
Eg1 my string
"#113#468#9999#2474#.#.#.#.#"
Result
2474
Eg2
"5743#788#1#9#89999#64#68#.#.#"
Result
68
Thanks in advance.
1 ACCEPTED SOLUTION
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Well:
data want;
set have;
do i=1 to countw(mystring,"#");
if scan(mystring,i,"#") ne "." then last_num=scan(mystring,i,"#");
end;
run;
7 REPLIES 7
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Note the last character could be a number or a .
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Below should do the job.
data have;
length string $100;
string="#113#468#9999#2474#.#.#.#.#";output;
string="5743#788#1#9#89999#64#68#.#.#";output;
string="#.#.#";output;
string="#.#.#123";output;
string="456#.#.#";output;
run;
data want;
set have;
number=input(scan(string,-1,,'kd'),best32.);
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Well:
data want;
set have;
do i=1 to countw(mystring,"#");
if scan(mystring,i,"#") ne "." then last_num=scan(mystring,i,"#");
end;
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
are your digits always surrounded by ##?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
An alternative..
data have;
length string $100;
string="#113#468#9999#2474#.#.#.#.#";output;
string="5743#788#1#9#89999#64#68#.#.#";output;
run;
data want;
set have;
to=anydigit(string, -length(string));
from=notdigit(string, -to)+1;
num=substr(string, from, to-from+1);
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thanks for all your help. So many solutions!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
No loop needed for your examples, very simple and straight forward-->
data want;
mystring="#113#468#9999#2474#.#.#.#.#";
last_num=scan(compress(mystring,'.'),-1,'#');
output;
mystring="5743#788#1#9#89999#64#68#.#.#";
last_num=scan(compress(mystring,'.'),-1,'#');
output;
run;