Help using Base SAS procedures

Convert char values to numeric values

Reply
Contributor
Posts: 22

Convert char values to numeric values

Hi all,

I am trying to remove "$", "," and then convert from char values to numeric values

amount (from input data)
$1,100.1
$1000
$59,000
$59

n_amount (the output)
1100.10
1000.00
59000.00
59.00

my codes:
attrib amount length=$10.
n_amount length=10.;

n_amount = input(amount,comma10.2);

However, the output I got:
n_amount
1100.1
10
590
0.59

Please help! Thank you.

-Lani
Super Contributor
Super Contributor
Posts: 3,174

Re: Convert char values to numeric values

Where is your INPUT statement logic to read up your data and assign a SAS character variable? It's best to share all your SAS code and ideally share it in a SAS session log (pasted in your post/reply).

You can add SAS PUTLOG _ALL_; commands to display all variable values - this may help you self-diagnose the situation.

Scott Barry
SBBWorks, Inc.
N/A
Posts: 0

Re: Convert char values to numeric values

Hi Lani,

See here you are assigning the length of the string as 10 to the variable char Amount.
When you apply the comma10.2 to read it in input statement it will take the last two digits as the digits after decimal point.
for example:

data test;
input chars $10.;
cards;
$1,100.100
$1000.0000
$59,000.00
$59.000000
;
run;

data temp;
set test;
newnum=input(chars,dollar10.2);
run;
proc print;
run;


Running the above code will give you the right result. since the length is exactly 10 chars.

If you want to get the desired result on the data which you have provided just use comma10. or dollar10. format and not the decimal part of the formats.
Try following this code:

data test;
input chars $10.;
cards;
$1,100.1
$1000
$59,000
$59
;
run;

data temp;
format newnum 10.2;
set test;
newnum=input(chars,dollar10.);
run;
proc print;
run;



Thanks,
Saurabh. Message was edited by: emerald85
Ask a Question
Discussion stats
  • 2 replies
  • 119 views
  • 0 likes
  • 3 in conversation