Chapter Contents

Previous

Next
FORMAT

FORMAT



Associates formats with variables

Valid: in a DATA step or PROC step
Category: Information
Type: Declarative


Syntax
Arguments
Details
Comparisons
Examples
Example 1: Assigning Formats and Defaults
Example 2: Removing a Format
See Also

Syntax

FORMAT variable(s) <format>
<DEFAULT=default-format>;

Arguments

variable
names one or more variables for SAS to associate with a format. You must specify at least one variable.
Tip: To disassociate a format from a variable, use the variable in a FORMAT statement without specifying a format in a DATA step or in PROC DATASETS. In a DATA step, place this FORMAT statement after the SET statement. See Removing a Format. You can also use PROC DATASETS.

format
specifies the format that is listed for writing the values of the variables.
Tip: Formats that are associated with variables by using a FORMAT statement behave like formats that are used with a colon modifier in a subsequent PUT statement. For details on using a colon modifer, see PUT, List.
See also: Formats by Category

DEFAULT=default-format
specifies a temporary default format for displaying the values of variables that are listed in the FORMAT statement. These default formats apply only to the current DATA step; they are not permanently associated with variables in the output data set.

A DEFAULT= format specification applies to

Default: If you omit DEFAULT=, SAS uses BESTw. as the default numeric format and $w. as the default character format.
Tip: A DEFAULT= specification can occur anywhere in a FORMAT statement. It can specify either a numeric default, a character default, or both.
Valid: in a DATA step
Featured in: Assigning Formats and Defaults


Details

The FORMAT statement can use standard SAS formats or user-written formats that have been previously defined in PROC FORMAT. A single FORMAT statement can associate the same format with several variables, or it can associate different formats with different variables. If a variable appears in multiple FORMAT statements, SAS uses the format that is assigned last.

You use a FORMAT statement in the DATA step to permanently associate a format with a variable. SAS changes the descriptor information of the SAS data set that contains the variable. You can use a FORMAT statement in some PROC steps, but the rules are different. For more information, see SAS Procedures Guide.


Comparisons

Both the ATTRIB and FORMAT statements can associate formats with variables, and both statements can change the format that is associated with a variable. You can use the FORMAT statement in PROC DATASETS to change or remove the format that is associated with a variable. You can also associate, change, or disassociate formats and variables in existing SAS data sets through the windowing environment.


Examples

Example 1: Assigning Formats and Defaults

This example uses a FORMAT statement to assign formats and default formats for numeric and character variables. The default formats are not associated with variables in the data set but affect how the PUT statement writes the variables in the current DATA step.

data tstfmt;
   format W $char3.
          Y 10.3
          default=8.2 $char8.;
   W='Good morning.';
   X=12.1;
   Y=13.2;
   Z='Howdy-doody';
   put W/X/Y/Z;
run;

proc contents data=tstfmt;
run;

proc print data=tstfmt;
run;

The following output shows a partial listing from PROC CONTENTS, as well as the report that PROC PRINT generates.

                         The SAS System                        3

                       CONTENTS PROCEDURE

      -----Alphabetic List of Variables and Attributes-----
 
         #    Variable    Type    Len    Pos    Format
         ----------------------------------------------
         1    W           Char      3     16    $CHAR3.
         3    X           Num       8      8           
         2    Y           Num       8      0    10.3   
         4    Z           Char     11     19           
                         The SAS System                        4

        OBS    W               Y      X          Z

         1     Goo        13.200    12.1    Howdy-doody

The default formats apply to variables X and Z while the assigned formats apply to the variables W and Y.

The PUT statement produces this result:

----+----1----+----2
Goo
   12.10
3.200
Howdy-do

Example 2: Removing a Format

This example disassociates an existing format from a variable in a SAS data set. The order of the FORMAT and the SET statements is important.

data rtest;
   set rtest;
   format x;
run;

See Also

Statement:

ATTRIB

The DATASETS Procedure in SAS Procedures Guide


Chapter Contents

Previous

Next

Top of Page

Copyright 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.