Sample 25987: Create a new data set with the attributes of an existing data set
To create a new data set from an existing data set so that all of the variable attributes are the same, use a conditional SET statement similar to "if 0 then set your_data_set_name ;" .
The program data vector is created during the compilation phase of the DATA step. The 'if 0 then set ...' is a Boolean false, so the SET statement does not execute to read the data in the data set. Instead, the header portion of the data set is read and the attributes of each variable are preserved into the newly created data set.
See the Full Code tab for sample code.
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
The data set EXIST is created in the first DATA step. The CONTENTS procedure lists all of the attributes of each variable in EXIST. In order to create a new data set named ADD with the same attributes as EXIST, the next DATA step incorporates a conditional SET statement referencing EXIST. The CONTENTS procedure that follows verifies that all of the attributes of EXIST were copied to ADD.
data exist;
length Date 4;
input #1 Name $18.
#2 Address1 & $30.
#3 Address2 & $30.
#4 Date : mmddyy10.
Height
Weight;
format date date9.;
datalines;
Hal Schneider
123 Front Street
Chicago, IL
10/21/2005 68 155
Jane Friedman
881 Blue Bird Court
Baltimore, MD
01/14/2002 60 101
;
run;
proc print data=exist;
title "Listing of EXIST";
run;
proc contents data=exist;
title "Data Description for EXIST";
run;
data add;
if 0 then set exist;
input Name & $50. Address1 & $50. Address2 & $50. Date : date9. Height Weight;
format date date9.;
datalines;
Roger Dodger 544 Lazy Brook Rd. Flemington, NJ 13May1999 72 233
Big Bird 123 Sesame Street Blue Bird, PA 05Jan2005 75 112
;
run;
proc contents data=add;
title "Attributes of ADD";
run;
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
Listing of EXIST
Obs Date Name Address1 Address2 Height Weight
1 21OCT2005 Hal Schneider 123 Front Street Chicago, IL 68 155
2 14JAN2002 Jane Friedman 881 Blue Bird Court Baltimore, MD 60 101
/* Partial output of PROC CONTENTS for EXIST */
Alphabetic List of Variables and Attributes
# Variable Type Len Format
3 Address1 Char 30
4 Address2 Char 30
1 Date Num 4 DATE9.
5 Height Num 8
2 Name Char 18
6 Weight Num 8
/* Partial output of PROC CONTENTS for ADD */
Alphabetic List of Variables and Attributes
# Variable Type Len Format
3 Address1 Char 30
4 Address2 Char 30
1 Date Num 4 DATE9.
5 Height Num 8
2 Name Char 18
6 Weight Num 8
This sample illustrates an easy way to copy the variable attributes from a SAS® data set to create a new data set with the same properties.
Type: | Sample |
Topic: | Non SAS Authors ==> Ron Cody SAS Reference ==> DATA Step Common Programming Tasks ==> Reading and Writing SAS Data
|
Date Modified: | 2014-05-07 09:34:01 |
Date Created: | 2005-11-22 16:59:07 |
Operating System and Release Information
SAS System | Base SAS | All | n/a | n/a |