I have a dataset DST1 which has two columns (ID and AMOUNT) and both are character field.
ID AMOUNT
A1 +100
A2 0
A3 +350
A4 -200
I am trying to convert the Amount column as number and remove the + sign for positive numbers and retain the sign for negative number only. Could you please help.
data have;
input ID $ AMOUNT $;
cards;
A1 +100
A2 0
A3 +350
A4 -200
;
data want;
set have;
New_amt=input(compress(amount,,'kd'),8.)*sign(amount);
run;
data have;
input ID $ AMOUNT $;
cards;
A1 +100
A2 0
A3 +350
A4 -200
;
data want;
set have;
_l=length(amount);
_infmt=cats('bz',_l,'.');
new_amount=inputn(amount,_infmt);
drop _:;
run;
However, not confident of this solution though
Most simplest:
data have;
input ID $ AMOUNT $;
cards;
A1 +100
A2 0
A3 +350
A4 -200
;
data want;
set have;
new_amt=input(amount,e8.);
run;
I only learned e informat just now
data have;
input ID $ AMOUNT $;
cards;
A1 +100
A2 0
A3 +350
A4 -200
;
data want;
set have;
New_amt=input(compress(amount,'-','kd'),best.);
run;
An existing variable cannot change types. To create a numeric value you will need to create a new variable.
If you want to use the same name of the variable as the new variable name then modify @novinosrin solution as
data want; set have (rename=(amount=oldamount)); new_amt=input(oldamount,e8.); drop oldamount; run;
data have;
input ID $ AMOUNT $;
cards;
A1 +100
A2 0
A3 +350
A4 -200
;
data want;
set have;
New_amt=input(amount,best.);
run;
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 25. 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.