BookmarkSubscribeRSS Feed
SSG
Fluorite | Level 6 SSG
Fluorite | Level 6

Hi,

I've imported a csv with dollar values that have $ and commas (e.g. $1,000). This imports as a character variable in SAS 9.4 (which is what I'm using). I'm using a data step and COMPRESS to try to eliminate the commas and $ so I can then convert this to a numeric field.  Here's the gist of what I have

 

data file_name;set file_name;

newvar=compress(oldvar,'$',',');

run; [then in a subsequent data step I convert it to numeric)

 

This eliminates the $, but the commas remain (i.e. it would now read '1,000'). I also get the following warning:

 

WARNING: In a call to the COMPRESS function or routine, the modifier "," not
valid.

 

There is no possible way I am the only SAS user with this problem, but I can't seem to find a solution anywhere. What am I doing wrong? Thanks in advance!

3 REPLIES 3
Astounding
PROC Star

If you are wondering why nobody else has posted the question, it's because there's an easier way to solve the problem.  You don't need COMPRESS at all.  In a DATA step, you could use:

 

newvar = input(oldvar, dollar16.);

 

It will read the numerics, and ignore the commas and dollar signs automatically.

 

But to answer your question, you would need to include the list of characters to remove in a single string:

 

newvar=compress(oldvar,'$,');

SSG
Fluorite | Level 6 SSG
Fluorite | Level 6

Well, I'd tried dollar8. before I went down this road and I got an error indicating dollar8. was an invalid format; but for some reason dollar16. worked.  Thanks!

Reeza
Super User

You put both characters in the second parameter.

newvar = compress(oldvar, '$,');

The third parameter is modifiers, please read the documentation. For example putting 'a' as the third parameter would remove all alphabetic variables. I think you could also modify your call to be:

newVar = compress(oldvar, , 'kn');
which would mean keep all digits, underscores.

https://documentation.sas.com/?docsetId=lefunctionsref&docsetTarget=n0fcshr0ir3h73n1b845c4aq58hz.htm...

 


@SSG wrote:

Hi,

I've imported a csv with dollar values that have $ and commas (e.g. $1,000). This imports as a character variable in SAS 9.4 (which is what I'm using). I'm using a data step and COMPRESS to try to eliminate the commas and $ so I can then convert this to a numeric field.  Here's the gist of what I have

 

data file_name;set file_name;

newvar=compress(oldvar,'$',',');

run; [then in a subsequent data step I convert it to numeric)

 

This eliminates the $, but the commas remain (i.e. it would now read '1,000'). I also get the following warning:

 

WARNING: In a call to the COMPRESS function or routine, the modifier "," not
valid.

 

There is no possible way I am the only SAS user with this problem, but I can't seem to find a solution anywhere. What am I doing wrong? Thanks in advance!


 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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
  • 3 replies
  • 2024 views
  • 2 likes
  • 3 in conversation