BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
suncawy
Obsidian | Level 7

Hello,

  I'm needing to extract the middle numbers from an alphanumeric string that can have varying lengths.

This an example of the alphanumeric strings:

    Have:

             1)  SC_NATLTAP_P_617686046__0520 

             2)  NC_OPTUM PARTD_R_220335038421000999P__0910

 

  Want:

            1)  617686046

            2)  220335038421000999P

 

I've used Compress, but still leaves the last 4 digits, which I don't need.

ID = Compress(clm_nb,"_","A");

Thank you in advance.

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
average_joe
Obsidian | Level 7

This assumes the substring you want always begins with the first digit in the string.

 

 

data have;
infile cards;
input x $ string &:$50.;
cards;
1)  SC_NATLTAP_P_617686046__0520 
2)  NC_OPTUM PARTD_R_220335038421000999P__0910
;
run;

data want;
set have;
start = anydigit(string);  * find location of first digit;
num = scan(substr(string, start), 1, '_');  * extract substring that end with '_'; 
drop x start;
run;

 

 

View solution in original post

1 REPLY 1
average_joe
Obsidian | Level 7

This assumes the substring you want always begins with the first digit in the string.

 

 

data have;
infile cards;
input x $ string &:$50.;
cards;
1)  SC_NATLTAP_P_617686046__0520 
2)  NC_OPTUM PARTD_R_220335038421000999P__0910
;
run;

data want;
set have;
start = anydigit(string);  * find location of first digit;
num = scan(substr(string, start), 1, '_');  * extract substring that end with '_'; 
drop x start;
run;

 

 

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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
  • 1 reply
  • 613 views
  • 1 like
  • 2 in conversation