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

Here is my data:

E90K
G1006AfsX51
W1001X
R1014X
D102A
G1036D
R1035G fsX22 
R1035fsX1056

C52AfsX8

 

I want:

90

1006

1001

1014

102

1036

1035

1035

52

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

One simple way is to use SCAN() and tell that everything except digits are the delimiter characters.

So to get it as another character variable use something like this:

data want ;
  set have ;
  numb = scan(string,1,compress(string,,'d'));
run;

Or add an INPUT() function call to convert it to an actual number.

data want ;
  set have ;
  numb = input(scan(string,1,compress(string,,'d')),??32.);
run;
Obs    string          numb

  1    E90K              90
  2    G1006AfsX51     1006
  3    W1001X          1001
  4    R1014X          1014
  5    D102A            102
  6    G1036D          1036
  7    R1035G fsX22    1035
  8    R1035fsX1056    1035
  9                       .
 10    C52AfsX8          52

View solution in original post

6 REPLIES 6
PaigeMiller
Diamond | Level 26

So, does the numeric always begin in the second character? If so

 

data want;
    set have;
    string=substr(string,2); /* Throw out first character, we don't need it */
    numeric=input(substr(string,1,notdigit(string)-1),10.0);
run;
--
Paige Miller
achen
Calcite | Level 5

There are a few cases start with a number

448 insG
639+5G>A

 

I want:

448

639

PaigeMiller
Diamond | Level 26

@achen wrote:

There are a few cases start with a number

448 insG
639+5G>A

 

I want:

448

639


Are there any other cases we need to know about?

--
Paige Miller
Tom
Super User Tom
Super User

One simple way is to use SCAN() and tell that everything except digits are the delimiter characters.

So to get it as another character variable use something like this:

data want ;
  set have ;
  numb = scan(string,1,compress(string,,'d'));
run;

Or add an INPUT() function call to convert it to an actual number.

data want ;
  set have ;
  numb = input(scan(string,1,compress(string,,'d')),??32.);
run;
Obs    string          numb

  1    E90K              90
  2    G1006AfsX51     1006
  3    W1001X          1001
  4    R1014X          1014
  5    D102A            102
  6    G1036D          1036
  7    R1035G fsX22    1035
  8    R1035fsX1056    1035
  9                       .
 10    C52AfsX8          52
achen
Calcite | Level 5

Tom, thank you and it works great!

 

Ksharp
Super User

Tom,

scan() has built-in modified option for this kind of situation.

 

data have;
input string $20.;
cards;
E90K
G1006AfsX51
W1001X
R1014X
D102A
G1036D
R1035G fsX22 
R1035fsX1056
C52AfsX8
;
data want ;
  set have ;
  numb = scan(string,1,,'kd');
run;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 6 replies
  • 479 views
  • 2 likes
  • 4 in conversation