Desktop productivity for business analysts and programmers

Convert positive number to negative and negative to positive

Reply
N/A
Posts: 1

Convert positive number to negative and negative to positive

I need to convert a column of numbers to the opposite signs (positive to negative and negative to positive). I have been able to set up a computed column using the case function. However some of the values are zeros and nulls and these are coming out as "1". Any ideas or other functions I could use?

Thanks!
Valued Guide
Posts: 2,111

Re: Convert positive number to negative and negative to positive

Just multiply by -1 .
Valued Guide
Posts: 2,174

Re: Convert positive number to negative and negative to positive

- (minus) can be a "unary operator", like [pre] fall = - rise ; [/pre] It is also referred to as a "prefix operator" in the doc at http://support.sas.com/documentation/cdl/en/lrcon/61722/HTML/default/a000780367.htm .

PeterC
Valued Guide
Posts: 2,111

Re: Convert positive number to negative and negative to positive

If the var is called "x", then

x = -x;

changes the sign and you don't create a new variable. In the context of an assignment statement (data step), "=" means "assign the value of the expression on the right to the variable on the left". Similar to the SQL 'as' clause.
Contributor
Posts: 33

Re: Convert positive number to negative and negative to positive

how can I convert the negative numbers to positive only without changing positive numbers?

 

your help will be appreciated

 

Thanks

Community Manager
Posts: 2,696

Re: Convert positive number to negative and negative to positive

@lalohg You mean, take the absolute value?  Use the ABS() function:

 

x = abs(x);
Contributor
Posts: 33

Re: Convert positive number to negative and negative to positive

Yes!!!  Thanks Chris

Ask a Question
Discussion stats
  • 6 replies
  • 6922 views
  • 1 like
  • 5 in conversation