• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / SAS / SAS Dollar Format – Formatting Numbers as Dollars in SAS Dataset

SAS Dollar Format – Formatting Numbers as Dollars in SAS Dataset

January 21, 2022 Leave a Comment

In SAS, we can format numbers as dollars very easily using the built-in SAS DOLLARw.d format or creating our own format with PROC Format.

/* Using Built-in SAS DOLLARw.d format */
data data_new;
    format dollar_amount dollar10.2;
    set data;
run;

/* Creating our own dollar format */

proc format;
   picture our_dollar_format 
       low           - -1000000000 = '00,000,000,009.9 B' (prefix='-$' mult=.000000001)
       -1000000000 < - -1000000    = '00,000,009.9 M'     (prefix='-$' mult=.000001)
       -1000000    < - -1000       = '009.9 K'            (prefix='-$' mult=.01)
       -1000       - < 0           = '009.99'             (prefix='-$')
        0          - < 1000        = '009.99'             (prefix='$')
        1000       - < 1000000     = '009.9 K'            (prefix='$' mult=.01)
        1000000    - < 1000000000  = '00,000,009.9 M'     (prefix='$' mult=.000001)
        1000000000 -   high        = '00,000,000,009.9 B' (prefix='$' mult=.000000001)
;
run;

data data_new;
    format dollar_amount our_dollar_format;
    set data;
run;

When working with data which represents money, many times we want to display the data in a way that makes sense. For money, this means adding dollar signs, commas, and other things that makes the financial data more readable.

In SAS, we can use the built-in DOLLARw.d format to format numeric values with a leading dollar sign, a comma that separates every three digits, and a period that separates the decimal fraction.

Let’s say we have some data which represents a dollar amount. We can use the DOLLARw.d format to format the data to show the data in dollars.


data data;
    format num dollar16.2;
    input num;
    datalines;
10382741.23
  817293.12
     754.89
     340.40
   78701.28
23074813.74
 6431782.00
    4832.93
;
run;

              num
1  $10,382,741.23
2     $817,293.12
3         $754.89
4         $340.40
5      $78,701.28
6  $23,074,813.74
7   $6,431,782.00
8       $4,832.93

The downside of using the built-in dollar format is you need to know how big the biggest number is in your dataset, or else the format won’t apply.

For example, if we used the Dollar10.2 format, the first and sixth numbers wouldn’t be formatted correctly.

Creating a User-Defined SAS Dollar Format with PROC Format

With PROC format, we are able to create user-defined number formats so our data can be displayed cleaner. One such format is a currency format, or in particular, a dollar format.

We could use the standard SAS dollar format, but if you’ve ever worked with the built-in SAS formats, sometimes you want a little more.

With PROC format, we can build our own dollar format.

When viewing financial data, personally, I like to convert what I’m looking at into thousands, millions or billions, depending on the value of the number.

We can create a format for this easily with PROC format in the following way. We will create a picture format for displaying dollars with the following SAS code.

proc format;
   picture our_dollar_format 
       low           - -1000000000 = '00,000,000,009.9 B' (prefix='-$' mult=.000000001)
       -1000000000 < - -1000000    = '00,000,009.9 M'     (prefix='-$' mult=.000001)
       -1000000    < - -1000       = '009.9 K'            (prefix='-$' mult=.01)
       -1000       - < 0           = '009.99'             (prefix='-$')
        0          - < 1000        = '009.99'             (prefix='$')
        1000       - < 1000000     = '009.9 K'            (prefix='$' mult=.01)
        1000000    - < 1000000000  = '00,000,009.9 M'     (prefix='$' mult=.000001)
        1000000000 -   high        = '00,000,000,009.9 B' (prefix='$' mult=.000000001)
;
run;

The “prefix” option adds the “$” sign to the number, and the “mult” option ensures our number is scaled appropriately.

Let’s see how “our_dollar_format” is applied in practice.

data data;
    format num our_dollar_format.;
    input num;
    datalines;
10382741.23
  817293.12
     754.89
     340.40
   78701.28
23074813.74
 6431782.00
    4832.93
;
run;

/* Output */
        num
1    $1.0 M	
2  $817.2 K	
3   $754.89	
4   $340.40	
5   $78.7 K	
6    $2.3 M	
7    $0.6 M	
8    $4.8 K

The dollar values above are now formatted much cleaner.

Other Articles You'll Also Like:

  • 1.  SAS floor – Round Down to Floor of Number in a SAS Data Step
  • 2.  How to Select First 100 Observations of SAS Dataset
  • 3.  SAS include – Execute Code from Other Files in SAS with %include
  • 4.  Identifying Duplicates in SAS with PROC SORT dupout Option
  • 5.  SAS mean() Function – Find Average Across Columns in Data Step
  • 6.  SAS Remove Formats from Dataset with PROC DATASETS
  • 7.  SAS let – Create User-Defined Macro Variables in Your SAS Code
  • 8.  Get the Row Number in a SAS Data Step with _n_
  • 9.  countw SAS – Count Number of Words in a String
  • 10.  IN in SAS – Checking if Variable is in Array of Values

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy