Different Formats in SAS

shreyansh

Member
Staff member
What Are Formats in SAS?
Formats in SAS are used to control how data values are displayed rather than how they are stored.
They can be applied in a number of ways, including :Displaying numeric values in a readable way (e.g., currency, percent)
Grouping categories
Formatting dates and times.

FORMAT Syntax:
FORMAT variable format.;
  • variable is name of the column you want to format.
  • format. is name of the format you're applying (with a period).

Format Types : Standard SAS Formats
Date and Time Formats

FormatExample OutputDescription
DATE9.01MAY2025Date format (DDMMMYYYY)
MMDDYY10.05/01/2025US-style date
TIME5.10:20Hours and minutes
DATETIME20.01MAY2025:10:20:00Full datetime

Numeric Formats
FormatExample OutputDescription
DOLLAR8.$10,512Adds dollar sign and commas
COMMA6.10,512Adds comma to large numbers
PERCENT8.55.00%Multiplies by 100 and adds %
BEST.10.876543Best readable format (default)

Character Formats
Usually character formats are not displayed like numerics, but you could use formats to assign codes to labels (using user-defined formats).

User Defined Formats
You can create user-defined formats using PROC FORMAT. Formats can create your mapping with values, whether those be numeric or characters.

Sample Code 1: Mapping Codes to Labels
proc format;
value genderfmt
1 = 'M'
2 = 'F'
. = 'Miss';
run;

data intellectsage;
input name $ gender;
format gender genderfmt.;
datalines;
Jain 1
Shan 2
Amex .
;
run;

Output:
1744708354849.png


Sample Code 2: Character Format
proc format;
value $statusfmt
'ACT' = 'Active'
'INACT' = 'Inactive'
'SUS' = 'Suspended';
run;

data intellectsage;
input id $ status $;
format status $statusfmt.;
datalines;
201 ACT
202 INACT
203 SUS
;
run;

Output:
1744708562997.png


Temporary Vs Permanent Formats
Temporary: Defined in your current SAS session with PROC FORMAT.
Permanent: Saved using a LIBRARY reference allowing you to access it in the future.

libname sage 'C:\formats';

proc format library=sage;
value agegroup
0 - 12 = 'Child group'
13 - 19 = 'Teen group'
20 - high = 'Adult group';
run;

To execute or call above command use below code
options fmtsearch=(sage);

Viewing and Managing Formats:

List formats:
proc format library=intellectsage fmtlib;
run;

Remove a format from a variable:
format varname;

Check applied formats in data dictionary:
proc contents data=intellectsage;
run;

Note:
Utilize formats to make reports less complicated in format without impacting the data.
You can combine with PROC FREQ, PROC PRINT, PROC REPORT, etc., to create clearer outputs.
As mentioned above, custom formats are a neat way of grouping continuous data into categories (for example, age bands, scales of income).
 
Back
Top