## Calculate Percentage incorrect ??

Solved
Frequent Contributor
Posts: 80

# Calculate Percentage incorrect ??

Hi,

I'm using SAS EG 4.3 with Windows 7 & am trying to perform a calculation of a % of a Currency amount.

Example, I have 2 Fileds on a table, Sales (Currency) & Allocation (%) - NOTE the datatypes appear as shown ie; \$ & %.

I go to Query Builder >> Add the 2 Fields to the Builder >> go to Add Computed Column >> Select Advanced Expression & enter;

t1.Sales*Allocation.

The figures for this example are Currency is \$180.00 & the Percentage is 22%.

The result for 22% of \$180.00 should be \$39.60 but I am getting \$38.74 ??

I then reloaded my % Field as a Number datatype & tried the below;

t1.Sales*(Allocation/100) same result ...

My goal is to apply the % Allocation figure against the sales figure to get the % of the Sales in this new Column.

Any help appreciated.

Accepted Solutions
Solution
‎10-14-2013 11:40 PM
Super User
Posts: 8,264

## Re: Calculate Percentage incorrect ??

Posted in reply to pcfriendly

I cannot see how entering the number as 22 (or really 0.22) could cause it to get that far off.

If you are storing the value in a permanent dataset make sure that the length of the variable is 8.  On machines that use IEEE floating point (unix and windows) you can store numbers in as little as 3 bytes, but then SAS just throws away the rest of the bits used to represent the floating point number.  If you try to store .22 into just 3 bytes when you read it back you get. 0.2199707031.

On an IBM mainframe you can even define a numeric variable as length 2, but even then it should not get as low as 0.21522222.

All Replies
Super User
Posts: 8,264

## Re: Calculate Percentage incorrect ??

Posted in reply to pcfriendly

From the \$ amount you are getting it looks the percentage is less than 22% and it is just being rounded in your display.

s=180 p=0.22 n=39.6

s=180 p=0.2152222222 n=38.74

Frequent Contributor
Posts: 80

## Re: Calculate Percentage incorrect ??

Thanks Tom,

The number is being entered as 22 though?

Is there a way to make it not round the number?

Solution
‎10-14-2013 11:40 PM
Super User
Posts: 8,264

## Re: Calculate Percentage incorrect ??

Posted in reply to pcfriendly

I cannot see how entering the number as 22 (or really 0.22) could cause it to get that far off.

If you are storing the value in a permanent dataset make sure that the length of the variable is 8.  On machines that use IEEE floating point (unix and windows) you can store numbers in as little as 3 bytes, but then SAS just throws away the rest of the bits used to represent the floating point number.  If you try to store .22 into just 3 bytes when you read it back you get. 0.2199707031.

On an IBM mainframe you can even define a numeric variable as length 2, but even then it should not get as low as 0.21522222.

Frequent Contributor
Posts: 80

## Re: Calculate Percentage incorrect ??

OK, The length of the field is 8, the Type is Number & the Source Informat / Output Format / Output Informat are all PERCENT6.

Could the PERCENT6 be the problem?

Frequent Contributor
Posts: 80

## Re: Calculate Percentage incorrect ??

Posted in reply to pcfriendly

Thanks Tom,

I changed the PERCENT6 TO PERCENT8 & it now calculates correctly.

Many thanks !!

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
• 5 replies
• 789 views
• 3 likes
• 2 in conversation