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;

SAS Innovate 2025: Call for Content

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 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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