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!
title1 '12.2.2 Working with Fractional Values';
title2 'Showing Decimals';
proc format;
picture showval
other = '0000';
picture withdec
other = '00.0';
picture twodec
other = '09.00';
run;
1
title1 '12.2.2 Working with Fractional Values';
2
title2 'Showing Decimals';
3
4
PROC FORMAT;
5
picture showval
6
other = '0000';
7
picture withdec
8
other = '00.0';
9
picture twodec
10
other = '09.00';
11
RUN;
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!
data vallist;
do val = 0 to 3 by .25;
val2 = val;
val3 = val;
val4 = val;
output;
end;
format val2 showval. val3 withdec. val4 twodec.;
run;
1
DATA vallist;
2
DO val = 0 to 3BY .25;
3
val2 = val;
4
val3 = val;
5
val4 = val;
6
OUTPUT;
7
END;
8
FORMAT val2 showval. val3 withdec. val4 twodec.;
9
RUN;
3 Code Block
PROC PRINT
Explanation : Display of the 'vallist' table content to verify format rendering.
Copied!
proc print data=vallist;
run;
1
PROC PRINTDATA=vallist;
2
RUN;
4 Code Block
PROC FORMAT
Explanation : Definition of a 'showdec' format displaying two decimals. Without the 'round' option, this format will truncate values.
Copied!
proc format;
picture showdec
other = '09.00';
run;
1
PROC FORMAT;
2
picture showdec
3
other = '09.00';
4
RUN;
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!
data x;
do x = .007,.017,.123,1.234, 12.345, 1234;
y=x;
output;
end;
format y showdec. x 8.3;
run;
1
DATA x;
2
DO x = .007,.017,.123,1.234, 12.345, 1234;
3
y=x;
4
OUTPUT;
5
END;
6
FORMAT y showdec. x 8.3;
7
RUN;
6 Code Block
PROC PRINT
Explanation : Display to demonstrate default decimal truncation (e.g., 1.234 displayed as 1.23).
Copied!
proc print data=x;
run;
1
PROC PRINTDATA=x;
2
RUN;
7 Code Block
PROC FORMAT
Explanation : Definition of the 'showdecr' format using the '(round)' option to force arithmetic rounding instead of truncation.
Copied!
proc format;
picture showdecr (round)
other = '00009.00';
run;
1
PROC FORMAT;
2
picture showdecr (round)
3
other = '00009.00';
4
RUN;
8 Code Block
DATA STEP Data
Explanation : Recreation of the 'x' dataset and application of the new format with rounding.
Copied!
data x;
do x = .007,.017,.123,1.234, 12.345, 1234;
y=x;
output;
end;
format y showdecr.
x 8.3;
run;
1
DATA x;
2
DO x = .007,.017,.123,1.234, 12.345, 1234;
3
y=x;
4
OUTPUT;
5
END;
6
FORMAT y showdecr.
7
x 8.3;
8
RUN;
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!
proc print data=x;
run;
1
PROC PRINTDATA=x;
2
RUN;
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.
SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. ® indicates USA registration. WeAreCAS is an independent community site and is not affiliated with SAS Institute Inc.
This site uses technical and analytical cookies to improve your experience.
Read more.