BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Go210
Obsidian | Level 7
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.
1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Well:

data want;
  set have;
  do i=1 to countw(mystring,"#");
if scan(mystring,i,"#") ne "." then last_num=scan(mystring,i,"#");
end;
run;

View solution in original post

7 REPLIES 7
Go210
Obsidian | Level 7
Note the last character could be a number or a .
Patrick
Opal | Level 21

@Go210

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;
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Well:

data want;
  set have;
  do i=1 to countw(mystring,"#");
if scan(mystring,i,"#") ne "." then last_num=scan(mystring,i,"#");
end;
run;
PeterClemmensen
Tourmaline | Level 20

are your digits always surrounded by ##? 

PeterClemmensen
Tourmaline | Level 20

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;
Go210
Obsidian | Level 7

Thanks for all your help. So many solutions!

novinosrin
Tourmaline | Level 20

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;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 1935 views
  • 1 like
  • 5 in conversation