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;
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.