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

I import many different data files into SAS.  Some of the phone numbers have dashes, some do not.  How can I remove the dashes from the phone numbers for all different data types?  I get an error when I use COMPRESS on numeric data types.

This is what I would like to do:

proc sql;
create table test as
select

COMPRESS(phone_number,'-') as PhoneNumber

from MyTable
;quit;

But this won't work when there isn't already a dash, because it's numeric.  I tried converting it to a char first, but if the phone number is already a char, I can't do that either.

I haven't found a good way to check if a field is numeric or char value yet, either.  Otherwise something like this would work:

select

case when ISNUMERIC(phone_number) then phone_number else COMPRESS(phone_number,'-') end as PhoneNumber

But obviously ISNUMERIC does not exist.  Any ideas?  Thanks for your help!

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Use CATS() function. It will handle both character and numeric arguments without issuing warnings.  You might want to set a length of your new variable, otherwise it will probably default to 200.

COMPRESS(cats(phone_number),'-') as PhoneNumber length=10

View solution in original post

7 REPLIES 7
Louis44
Calcite | Level 5

It turns out the CASE statement doesn't work because COMPRESS requires a char expression (even if it's not executed).  And there are formatting issues.

Any way around this?

Is there any way to get COMPRESS to work for both char and numeric values?  Or is there anyway to convert any data type to a char, even if it is already a char?

To sum up, say I have two files where the phone numbers are different data types:

File 1 phone number:  1234567890     (data type numeric)

File 2 phone number:  123-456-7890   (data type char)

For both, the output needs to be 1234567890

Reeza
Super User

Step 1 - Convert all to characters

Step 2 - Compress out anything that's not numbers

Step 3 - Format as character.

Louis44
Calcite | Level 5

When converting all to characters (step 1 above), should I use PUT?  Because PUT does not work with a value that's already char datatype.  I tried to convert the field to char in a separate data step, but when I got to the proc sql, it thought it was numeric again.

Reeza
Super User

I think we, or at least I, need some more info on what your trying to do and what code you're using. 

Tom
Super User Tom
Super User

Use CATS() function. It will handle both character and numeric arguments without issuing warnings.  You might want to set a length of your new variable, otherwise it will probably default to 200.

COMPRESS(cats(phone_number),'-') as PhoneNumber length=10

Louis44
Calcite | Level 5


CATS works perfectly!  That's exactly what I needed, it works on both sets of data.  Thank you!

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 1022 views
  • 0 likes
  • 3 in conversation