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;
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
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.