Sample 56698: Create a format to write numbers in engineering format style
Engineering notation is a version of scientific notation in which the exponent of ten must be divisible by three.
There is not a format available in SAS® software to write this style of output. However, you can create a function with the FCMP procedure and call it as a label in PROC FORMAT.
Click the Full Code tab to see an example.
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.
In the sample code below, the FCMP procedure creates a function to format numeric values with an exponent as a multiple of three, and PROC FORMAT uses the function as a label. The format is applied to the sample data in the DATA _NULL_ step. The results are written to the SAS log, and are shown on the
Results tab.
proc fcmp outlib=work.myfncs.mathfncs;
function engfmt(x) $10;
length text $10;
/* Use the E format */
text = left(putn(x,'e10.3'));
/* Proceed if 'E' is found as an exponent indicator */
i = index(text,'E');
if i<=1 then goto doret;
/* Extract the exponent and mantissa, and see if the */
/* exponent is a multiple of 3 */
exp=inputn(substr(text,i+1),'best12.');
man=inputn(substr(text,1,i-1),'best12.');
j=mod(exp,3);
/* If the exponent is not a multiple of 3, */
/* adjust the mantissa and exponent */
do while(j ne 0);
man=man*10;
exp=exp-1;
j=mod(exp,3);
end;
/* Recreate the text with the revised mantissa and exponent */
text=cats(man,'E',exp);
doret:;
return (text);
endsub;
run;
options cmplib=work.myfncs;
proc format;
value engfmt(default=10) other=[engfmt()];
run;
/* Data is created and formatted to demonstrate that the new format */
/* writes data to the SAS log in engineering format style */
data _null_;
input x;
put x= x=e10.3 x=engfmt.;
datalines;
1234
-0.03
2.4e2
2.4e-2
-2.4e2
-2.4e-2
7.8e7
.e
-3.7e7
3.7e-7
;
run;
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.
/* The results from the PUT statement in the SAS log */
x=1234 x=1.234E+03 x=1.234E3
x=-0.03 x=-3.000E-02 x=-30E-3
x=240 x=2.400E+02 x=240E0
x=0.024 x=2.400E-02 x=24E-3
x=-240 x=-2.400E+02 x=-240E0
x=-0.024 x=-2.400E-02 x=-24E-3
x=78000000 x=7.800E+07 x=78E6
x=E x=E x=E
x=-37000000 x=-3.700E+07 x=-37E6
x=3.7E-7 x=3.700E-07 x=370E-9
| Type: | Sample |
| Topic: | SAS Reference ==> Formats ==> Numeric
|
| Date Modified: | 2015-10-05 14:47:18 |
| Date Created: | 2015-10-01 10:30:45 |
Operating System and Release Information
| SAS System | Base SAS | z/OS | 9.2 TS1M0 | |
| Microsoft® Windows® for 64-Bit Itanium-based Systems | 9.2 TS1M0 | |
| Microsoft Windows Server 2003 Datacenter 64-bit Edition | 9.2 TS1M0 | |
| Microsoft Windows Server 2003 Enterprise 64-bit Edition | 9.2 TS1M0 | |
| Microsoft Windows XP 64-bit Edition | 9.2 TS1M0 | |
| Microsoft® Windows® for x64 | 9.2 TS1M0 | |
| Microsoft Windows Server 2003 Datacenter Edition | 9.2 TS1M0 | |
| Microsoft Windows Server 2003 Enterprise Edition | 9.2 TS1M0 | |
| Microsoft Windows Server 2003 Standard Edition | 9.2 TS1M0 | |
| Microsoft Windows XP Professional | 9.2 TS1M0 | |
| Windows Vista | 9.2 TS1M0 | |
| Windows Vista for x64 | 9.2 TS1M0 | |
| 64-bit Enabled AIX | 9.2 TS1M0 | |
| 64-bit Enabled HP-UX | 9.2 TS1M0 | |
| 64-bit Enabled Solaris | 9.2 TS1M0 | |
| HP-UX IPF | 9.2 TS1M0 | |
| Linux | 9.2 TS1M0 | |
| Linux for x64 | 9.2 TS1M0 | |
| OpenVMS on HP Integrity | 9.2 TS1M0 | |
| Solaris for x64 | 9.2 TS1M0 | |