extracting numerals from a character field.

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

  1. mark b

    mark b Guest

    I am attempting extract a house no. from a variable that contains the
    first line of somebody's address

    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
    #1
    1. Advertisements

  2. 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.:
    One Hundred the new road
    B100 the new road
    100B the new road
    the new road

    Good luck,
    Dennis Diskin


    I am attempting extract a house no. from a variable that contains the
    first line of somebody's address

    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
    #2
    1. Advertisements

  3. mark b

    RolandRB Guest

    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;
    3 address="100 name of road";
    4 nondigits=compress(address,"0123456789");
    5 digits=compress(address,nondigits);
    6 put digits=;
    7 run;

    digits=100
     
    RolandRB, Jan 25, 2005
    #3
  4. mark b

    toby dunn Guest

    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 <>
    Reply-To: mark b <>
    To:
    Subject: extracting numerals from a character field.


    I am attempting extract a house no. from a variable that contains the
    first line of somebody's address

    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
    #4
  5. 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.

    But what happens if your address is:

    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
    #5
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.