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;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.