# extracting numerals from a character field.

Discussion in 'SAS (Statistical Analysis Software)' started by mark b, Jan 25, 2005.

1. ### mark bGuest

I am attempting extract a house no. from a variable that contains the

e.g var = 100 the new road.

For this record I would require new_var = 100.

Is there a SAS function that will easily allow this?

Substr on it's own does not work as the length of the house number is
not constant in all records.

As ever, all help greatly appreciated.

Thanks,

Mark

mark b, Jan 25, 2005

2. ### Dennis DiskinGuest

Mark,

When the data is well behaved, tou can just use INPUT and SCAN:
new_var=input(scan(var,1,' '),8.);

Having worked with this type of data, I can guarantee you'll get some that won't fit. Eg.:

Good luck,
Dennis Diskin

I am attempting extract a house no. from a variable that contains the

e.g var = 100 the new road.

For this record I would require new_var = 100.

Is there a SAS function that will easily allow this?

Substr on it's own does not work as the length of the house number is
not constant in all records.

As ever, all help greatly appreciated.

Thanks,

Mark

Dennis Diskin, Jan 25, 2005

3. ### RolandRBGuest

You could set up a temporary variable that is the address line
compressed for all numeric digits and then compress the original field
using the result to just leave you with the numeric digits. But it may
sometimes not do what you want.

1 data _null_;
2 length nondigits $200 digits$ 8;
6 put digits=;
7 run;

digits=100

RolandRB, Jan 25, 2005
4. ### toby dunnGuest

Mark,

In version 9.0 I think SAS came out with anydigit and anyalpha function that
I think will get you where you want to go. However, you more than likely
will have to use it with the substr function.

Toby Dunn

From: mark b <>
To:
Subject: extracting numerals from a character field.

I am attempting extract a house no. from a variable that contains the

e.g var = 100 the new road.

For this record I would require new_var = 100.

Is there a SAS function that will easily allow this?

Substr on it's own does not work as the length of the house number is
not constant in all records.

As ever, all help greatly appreciated.

Thanks,

Mark

toby dunn, Jan 25, 2005
5. ### David L. CassellGuest

I would recommend you try the Perl regular expression functions that are
now in SAS 9. That way, you could grab an address number of any length.

Apt. 42, 1313 Mockingbird Lane

Do you need to be able to handle that? How about:

417 1st Avenue
or
P.O. Box 41213

Just wondering,
David

David L. Cassell, Jan 26, 2005