BookmarkSubscribeRSS Feed
Jagadeesh2907
Obsidian | Level 7

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. 

 

6 REPLIES 6
novinosrin
Tourmaline | Level 20
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;
novinosrin
Tourmaline | Level 20
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

novinosrin
Tourmaline | Level 20

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 

rajeshalwayswel
Pyrite | Level 9
  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;
ballardw
Super User

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;
Ksharp
Super User
data have;
input ID $    AMOUNT $;
cards;
A1    +100
A2     0
A3     +350
A4     -200
;

data want;
set have;
New_amt=input(amount,best.);
run;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1860 views
  • 2 likes
  • 5 in conversation