Well:
data want;
set have;
do i=1 to countw(mystring,"#");
if scan(mystring,i,"#") ne "." then last_num=scan(mystring,i,"#");
end;
run;
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;
Well:
data want;
set have;
do i=1 to countw(mystring,"#");
if scan(mystring,i,"#") ne "." then last_num=scan(mystring,i,"#");
end;
run;
are your digits always surrounded by ##?
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;
Thanks for all your help. So many solutions!
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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.