Most people use the round function to round to the nearest decimal. There are other great ways to use the round function. You can use it to round to something other than the nearest decimal, such as to the nearest dollar value or to round times. The following code snipets provide examples of how you can expand your use of the round function.
Example showing how to round to the nearest decimal.
DATA _null_; cost = 4.99; units = 3; ucost = Round(cost/units,.01); PUT cost units ucost ; RUN;
Example showing how to round a single value to the nearest quarter, the nearest nickel, and the nearest $5.
DATA _null_; x = 234.2357222; x2 = Round(x, .25); * the nearest quarter; x3 = Round(x, .05); * the nearest nickel; x4 = Round(x, 5); * the nearest 5 dollars; Put x x2 x3 x4; RUN;
Example showing how to round times.
First a SAS datetime value is created using the DHMS function.
Then the ROUND function is used with the HMS function to round to the nearest 15 minutes, 7 1/2 minutes and 15 seconds.
DATA _null_; dt = dhms('12mar2003'D,14,23,10); * create a datetime - 2:23pm; dt2 = Round(dt,hms(0,15,00)); * round it to nearest 15 minutes; dt3 = Round(dt,hms(0,7,30)); * round it to nearest 7 1/2 minutes; dt4 = Round(dt,hms(0,0,15)); * to the nearest 15 seconds; Put (dt dt2 dt3 dt4) (DATETIME. ' '); RUN;
Example showing how to round times using datetime constants.
DATA _null_; dt = '12mar2003:14:23:10'DT; * create a datetime - 2:23pm; dt2 = Round(dt,'0:15:00'T); * round it to nearest 15 minutes; dt3 = Round(dt,'0:7:30'T); * round it to nearest 7 1/2 minutes; dt4 = Round(dt,'0:00:15'T); * to the nearest 15 seconds; Put (dt dt2 dt3 dt4) (DATETIME. ' '); RUN;
About the Author
Richard Wright began writing SAS code over 25 years ago keying code and data on punch cards as a student at University of Oregon. Since then he has worked at various agencies for the state of Texas. He is certified in Base and Advanced SAS.
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
Type: | Sample |
Topic: | SAS Reference ==> Functions ==> Truncation ==> ROUND |
Date Modified: | 2008-02-06 08:56:15 |
Date Created: | 2008-02-05 15:38:56 |
Product Family | Product | Host | SAS Release | |
Starting | Ending | |||
SAS System | Base SAS | AIX | ||
Microsoft® Windows® for x64 | ||||
OS/2 | ||||
Microsoft Windows 95/98 | ||||
Microsoft Windows 2000 Advanced Server | ||||
Microsoft Windows 2000 Datacenter Server | ||||
Microsoft Windows 2000 Server | ||||
Microsoft Windows 2000 Professional | ||||
Microsoft Windows NT Workstation | ||||
Microsoft Windows Server 2003 Datacenter Edition | ||||
Microsoft Windows Server 2003 Enterprise Edition | ||||
Microsoft Windows Server 2003 Standard Edition | ||||
Microsoft Windows XP Professional | ||||
Windows Millennium Edition (Me) | ||||
Windows Vista | ||||
64-bit Enabled AIX | ||||
64-bit Enabled HP-UX | ||||
64-bit Enabled Solaris | ||||
ABI+ for Intel Architecture | ||||
HP-UX | ||||
HP-UX IPF | ||||
IRIX | ||||
Linux | ||||
Linux on Itanium | ||||
OpenVMS Alpha | ||||
OpenVMS on HP Integrity | ||||
Solaris | ||||
Solaris for x64 | ||||
Tru64 UNIX | ||||
z/OS | ||||
OpenVMS VAX | ||||
Microsoft® Windows® for 64-Bit Itanium-based Systems | ||||
Microsoft Windows Server 2003 Datacenter 64-bit Edition | ||||
Microsoft Windows Server 2003 Enterprise 64-bit Edition | ||||
Microsoft Windows XP 64-bit Edition |