Modifying SAS Data Set Names and Variable Attributes |
Understanding How to Modify Variable Attributes |
Each variable in a SAS data set has attributes such as name, type, length, format, informat, label, and so on. These attributes enable you to identify a variable as well as define to SAS how the variable can be used.
By using the DATASETS procedure, you can assign, change, or remove certain attributes with the MODIFY statement and subordinate statements. For example, using MODIFY and subordinate statements enables you to
Note: You cannot use the MODIFY statement to modify fixed attributes such as the type or length of a variable.
Renaming Variables |
You might need to rename variables, for example, before combining data sets that have one or more matching variable names. The DATASETS procedure enables you to rename one or more variables by using the MODIFY statement and its subordinate RENAME statement. Here is the syntax for the statements:
MODIFY SAS-data-set;
|
SAS-data-set |
is the name of the SAS data set that contains the variable that you want to rename. |
old-name | |
new-name |
This example renames two variables in the data set HURRICANE, which is in the SAS data library USCLIM. The following statements change the variable name State to Place and the variable name Deaths to USDeaths. The DATASETS procedure is already active, so the PROC DATASETS statement is not necessary.
modify hurricane; rename State=Place Deaths=USDeaths; run;
The SAS log messages verify that the variables are renamed to Place and USDeaths as shown in the following output. All other attributes that are assigned to these variables remain unchanged.
Renaming Variables in the Data Set HURRICANE
38 modify hurricane; 39 rename State=Place Deaths=USDeaths; NOTE: Renaming variable State to Place. NOTE: Renaming variable Deaths to USDeaths. 40 run;
Assigning, Changing, or Removing Formats |
SAS enables you to assign and store formats, which are used by many SAS procedures for output. Assigning, changing, or removing a format changes the way the values are printed or displayed. By using the DATASETS procedure, you can change a variable's format with the MODIFY statement and its subordinate FORMAT statement. You can change a variable's format either to a SAS format or to a format that you have defined and stored, or you can remove a format. Here is the syntax for these statements:
MODIFY SAS-data-set;
|
When you assign or change a format, follow these rules:
List multiple variable names or use an abbreviated variable list if you want to assign the format to more than one variable.
The following FORMAT statement illustrates ways to include many variables and formats in the same FORMAT statement:
format Date1-Date5 date9. Cost1 Cost2 dollar4.2 Place $char25.;
The variables Date1 through Date5 are written in abbreviated list form, and the format DATE9. is assigned to all five variables. The variables Cost1 and Cost2 are listed individually before their format. The format $CHAR25. is assigned to the variable Place.
There are two rules when you are removing formats from variables:
Place the variable names last in the list if you are using the same FORMAT statement to assign or change formats.
For example, by using the SAS data set HURRICANE, the following statements change the format for the variable Date from a full spelling of the month, date, and year to an abbreviation of the month and year, remove the format for the variable Millions, and display the contents of the data set HURRICANE before and after the changes. Note that because the FORMAT statement does not send messages to the SAS log, you must use the CONTENTS statement if you want to make sure that the changes were made.
contents data=hurricane; modify hurricane; format Date monyy7. Millions; contents data=hurricane; run;
The following output from the two CONTENTS statements displays the contents of the data set before and after the changes. The format for the variable Date is changed from WORDDATE18. to MONYY7., and the format for the variable Millions is removed.
Modifying Variable Formats in the Data Set HURRICANE
The SAS System
The DATASETS Procedure
Data Set Name: USCLIM.HURRICANE Observations: 5
Member Type: DATA Variables: 5
Engine: V8 Indexes: 0
Created: 14:31 Wednesday, November 15, 2000 Observation Length: 48
Last Modified: 9:19 Thursday, November 16, 2000 Deleted Observations: 0
Protection: Compressed: NO
Data Set Type: Sorted: NO
Label:
-----Engine/Host Dependent Information-----
Data Set Page Size: 8192
Number of Data Set Pages: 1
First Data Page: 1
Max Obs per Page: 169
Obs in First Data Page: 5
Number of Data Set Repairs: 0
File Name: /u/userid/usclim/hurricane.sas7bdat
Release Created: 8.0202M0
Host Created: HP-UX
Inode Number: 14593
Access Permission: rw-r--r--
Owner Name: userid
File Size (bytes): 16384
-----Alphabetic List of Variables and Attributes-----
# Variable Type Len Pos Format Informat Label
------------------------------------------------------------------------
2 Date Num 8 0 WORDDATE18. DATE9.
4 Millions Num 8 16 DOLLAR6. Damage
5 Name Char 8 35
1 Place Char 11 24 $CHAR11.
3 USDeaths Num 8 8
The SAS System
The DATASETS Procedure
Data Set Name: USCLIM.HURRICANE Observations: 5
Member Type: DATA Variables: 5
Engine: V8 Indexes: 0
Created: 14:31 Wednesday, November 15, 2000 Observation Length: 48
Last Modified: 9:23 Thursday, November 16, 2000 Deleted Observations: 0
Protection: Compressed: NO
Data Set Type: Sorted: NO
Label:
-----Engine/Host Dependent Information-----
Data Set Page Size: 8192
Number of Data Set Pages: 1
First Data Page: 1
Max Obs per Page: 169
Obs in First Data Page: 5
Number of Data Set Repairs: 0
File Name: /u/userid/usclim/hurricane.sas7bdat
Release Created: 8.0202M0
Host Created: HP-UX
Inode Number: 14593
Access Permission: rw-r--r--
Owner Name: userid
File Size (bytes): 16384
-----Alphabetic List of Variables and Attributes-----
# Variable Type Len Pos Format Informat Label
--------------------------------------------------------------------
2 Date Num 8 0 MONYY7. DATE9.
4 Millions Num 8 16 Damage
5 Name Char 8 35
1 Place Char 11 24 $CHAR11.
3 USDeaths Num 8 8
Assigning, Changing, or Removing Labels |
A label is the descriptive information that identifies variables in tables, plots, and graphs. You usually assign labels when you create a variable. If you do not assign a label, then SAS uses the variable name as the label. However, in CONTENTS output, if a label is not assigned, then the field is blank. By using the MODIFY statement and its subordinate LABEL statement, you can assign, change, or remove a label. Here is the syntax for these statements:
MODIFY SAS-data-set;
|
When you use the LABEL statement, follow these rules:
Enclose the text of the label in single or double quotation marks. If a single quotation mark appears in the label (for example, an apostrophe), then enclose the text with double quotation marks.
Limit the label to no more than 256 characters, including blanks.
To remove a label, use a blank as the text of the label, that is, variable=' '.
For example, by using the SAS data set HURRICANE, the following statements change the label for the variable Millions and assign a label for the variable Place. Because the LABEL statement does not send messages to the SAS log, the CONTENTS statement is specified to verify that the changes were made. The QUIT statement stops the DATASETS procedure.
contents data=hurricane; modify hurricane; label Millions='Damage in Millions' Place='State Hardest Hit'; contents data=hurricane; run; quit;
The following output from the two CONTENTS statements displays the contents of the data set before and after the changes:
Modifying Variable Labels in the Data Set HURRICANE
The SAS System
The DATASETS Procedure
Data Set Name: USCLIM.HURRICANE Observations: 5
Member Type: DATA Variables: 5
Engine: V8 Indexes: 0
Created: 14:31 Wednesday, November 15, 2000 Observation Length: 48
Last Modified: 9:23 Thursday, November 16, 2000 Deleted Observations: 0
Protection: Compressed: NO
Data Set Type: Sorted: NO
Label:
-----Engine/Host Dependent Information-----
Data Set Page Size: 8192
Number of Data Set Pages: 1
First Data Page: 1
Max Obs per Page: 169
Obs in First Data Page: 5
Number of Data Set Repairs: 0
File Name: /u/userid/usclim/hurricane.sas7bdat
Release Created: 8.0202M0
Host Created: HP-UX
Inode Number: 14593
Access Permission: rw-r--r--
Owner Name: userid
File Size (bytes): 16384
-----Alphabetic List of Variables and Attributes-----
# Variable Type Len Pos Format Informat Label
--------------------------------------------------------------------
2 Date Num 8 0 MONYY7. DATE9.
4 Millions Num 8 16 Damage
5 Name Char 8 35
1 Place Char 11 24 $CHAR11.
3 USDeaths Num 8 8
The SAS System
The DATASETS Procedure
Data Set Name: USCLIM.HURRICANE Observations: 5
Member Type: DATA Variables: 5
Engine: V8 Indexes: 0
Created: 14:31 Wednesday, November 15, 2000 Observation Length: 48
Last Modified: 9:28 Thursday, November 16, 2000 Deleted Observations: 0
Protection: Compressed: NO
Data Set Type: Sorted: NO
Label:
-----Engine/Host Dependent Information-----
Data Set Page Size: 8192
Number of Data Set Pages: 2
First Data Page: 1
Max Obs per Page: 169
Obs in First Data Page: 5
Number of Data Set Repairs: 0
File Name: /u/userid/usclim/hurricane.sas7bdat
Release Created: 8.0202M0
Host Created: HP-UX
Inode Number: 14593
Access Permission: rw-r--r--
Owner Name: userid
File Size (bytes): 24576
-----Alphabetic List of Variables and Attributes-----
# Variable Type Len Pos Format Informat Label
--------------------------------------------------------------------------------
2 Date Num 8 0 MONYY7. DATE9.
4 Millions Num 8 16 Damage in Millions
5 Name Char 8 35
1 Place Char 11 24 $CHAR11. State Hardest Hit
3 USDeaths Num 8 8
Copyright © 2012 by SAS Institute Inc., Cary, NC, USA. All rights reserved.