In SAS, we can format numbers as percentages very easily using the built-in SAS PERCENTw.d format or creating our own format with PROC Format.
/* Using Built-in SAS PERCENTw.d format */
data data_new;
format num dollar10.2;
set data;
run;
/* Creating our own percent format */
proc format;
picture our_percent_format(round)
low -< 0 = '000009.9%' (prefix='-' mult=1000)
0 - high = '000009.9%' (mult=1000)
;
run;
data data_new;
format num our_percent_format;
set data;
run;
When working with data which represents percentages, many times we want to display the data in a way that makes sense. For most cases, this means adding percent signs and multiplying by 100 where appropriate.
In SAS, we can use the built-in PERCENTw.d format to format numeric values with a trailing percent sign and a value which is multiplied by 100.
Let's say we have some data which represents a percentage. We can use the PERCENTw.d format to format the data to show the data formatted like a percentage point.
data data;
format num percent10.2;
input num;
datalines;
0.123
1.789
0.504
0.981
0.48
;
run;
/* Output */
1 12.30%
2 178.90%
3 50.40%
4 98.10%
5 48.00%
Depending on how our data is set up, the built-in percent format can work, but other times it might make sense to create our own percent format.
Creating a User-Defined SAS Percent Format with PROC Format
With PROC format, we are easily able to define number formats so our data can be displayed cleaner. Another useful format is the percent format.
We could use the standard SAS percent format, but sometimes you want the ability to customize formats.
With PROC format, we can build our own percent format.
We can create a format for this easily with PROC format in the following way. We will create a picture format and pass the "round" option so that the format rounds the data first, and then displays the percents.
proc format;
picture our_percent_format(round)
low -< 0 = '000009.9%' (prefix='-' mult=1000)
0 - high = '000009.9%' (mult=1000)
;
run;
Depending on the scale of your data, you may want to change the multiplier.
Let's see how this looks when it is applied to some data.
data data;
format num our_percent_format.;
input num;
datalines;
0.123
1.789
0.504
0.981
0.48
;
run;
/* Output */
num
1 12.3%
2 178.9%
3 50.4%
4 98.1%
5 48.0%
As you can see from above, this looks much better. You can play with the multiplier and the format to get exactly what you want for your user defined percent format.
Hopefully this article has been helpful for you to learn how to use the built-in SAS format for percentages and how to create your own percent format in SAS.