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!
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,'$,');
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!
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!
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: