turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- Base SAS Programming
- /
- Round numbers and getting rid of extra zeroes

Topic Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

10-12-2016 03:09 PM

Hi,

suppose I have the following table PARMS:

parameter | esimate |

a | 14.72344 |

b | -0.00015 |

c | 0.74911 |

d | -0.00986 |

using the round function I rounded each value to 3 decimal places and got the following table PARMS2:

```
data parms2;
set parms;
estimate= round(estimate,.001);
run;
```

parameter | estimate |

a | 14.72300 |

b | 0 |

c | 0.74900 |

d | -0.01000 |

Now I want to get rid of the extra zeroes - I want to leave only the integer part and the first 3 decimals. I thought just getting rid of the 2 right-most numbers using the following code:

data parms3;

set parms2;

estimate=substr(estimate,1,length(estimate)-2);

run;

But the result PARMS3 is a mess:

parameter | estimate |

a | 14.70000 |

b | . |

c | 0.70000 |

d | 0 |

Could you please help me?

Thank you!

Accepted Solutions

Solution

10-12-2016
03:57 PM

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to ilikesas

10-12-2016 03:13 PM

SUBSTR is intended for characters you have a numeric variable.

Why not use a format instead?

`format estimate 12.3;`

All Replies

Solution

10-12-2016
03:57 PM

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to ilikesas

10-12-2016 03:13 PM

SUBSTR is intended for characters you have a numeric variable.

Why not use a format instead?

`format estimate 12.3;`

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to ilikesas

10-12-2016 03:21 PM

Note that using a format like 12.3 can remove the need for a separate rounding step and may prevent some odd behaviors involving multiple rounding.

Please see:

```
data _null_;
x= 123.78445;
r1=round(x,0.0001);
r2 = round(r1,0.001);
r3 = round(r2,0.01);
put x= 12.4 r1= x=12.3 r2= x=12.2 r3=;
run;
```