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;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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