Published on :
Reporting CREATION_INTERNE

Managing Decimals with PROC FORMAT PICTURE

This code is also available in: Deutsch Español Français
Awaiting validation
This script illustrates how to create custom picture formats to display fractional numbers. It covers displaying a specific number of decimals, the default truncation behavior of picture formats, and how to enable rounding with the (round) option.
Data Analysis

Type : CREATION_INTERNE


The 'vallist' and 'x' datasets are generated directly within the script via DO loops.

1 Code Block
PROC FORMAT
Explanation :
Definition of three custom 'picture' formats to control the display of numbers: integer (showval), one decimal (withdec), and two decimals (twodec).
Copied!
1title1 '12.2.2 Working with Fractional Values';
2title2 'Showing Decimals';
3 
4PROC FORMAT;
5 picture showval
6 other = '0000';
7 picture withdec
8 other = '00.0';
9 picture twodec
10 other = '09.00';
11RUN;
2 Code Block
DATA STEP Data
Explanation :
Creation of a test dataset 'vallist' containing values from 0 to 3 in steps of 0.25. Application of previously defined formats to observe display differences.
Copied!
1DATA vallist;
2 DO val = 0 to 3 BY .25;
3 val2 = val;
4 val3 = val;
5 val4 = val;
6 OUTPUT;
7 END;
8 FORMAT val2 showval. val3 withdec. val4 twodec.;
9RUN;
3 Code Block
PROC PRINT
Explanation :
Display of the 'vallist' table content to verify format rendering.
Copied!
1PROC PRINT DATA=vallist;
2RUN;
4 Code Block
PROC FORMAT
Explanation :
Definition of a 'showdec' format displaying two decimals. Without the 'round' option, this format will truncate values.
Copied!
1PROC FORMAT;
2 picture showdec
3 other = '09.00';
4RUN;
5 Code Block
DATA STEP Data
Explanation :
Creation of an 'x' dataset with specific fractional values to test the format's behavior on decimals.
Copied!
1DATA x;
2DO x = .007,.017,.123,1.234, 12.345, 1234;
3 y=x;
4 OUTPUT;
5END;
6FORMAT y showdec. x 8.3;
7RUN;
6 Code Block
PROC PRINT
Explanation :
Display to demonstrate default decimal truncation (e.g., 1.234 displayed as 1.23).
Copied!
1PROC PRINT DATA=x;
2RUN;
7 Code Block
PROC FORMAT
Explanation :
Definition of the 'showdecr' format using the '(round)' option to force arithmetic rounding instead of truncation.
Copied!
1PROC FORMAT;
2 picture showdecr (round)
3 other = '00009.00';
4RUN;
8 Code Block
DATA STEP Data
Explanation :
Recreation of the 'x' dataset and application of the new format with rounding.
Copied!
1DATA x;
2DO x = .007,.017,.123,1.234, 12.345, 1234;
3 y=x;
4 OUTPUT;
5END;
6FORMAT y showdecr.
7 x 8.3;
8RUN;
9 Code Block
PROC PRINT
Explanation :
Final display showing that values are now correctly rounded (e.g., 1.234 displayed as 1.23, but .007 displayed as .01).
Copied!
1PROC PRINT DATA=x;
2RUN;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.