Published on :

Examples: Create and Read SAS Data Sets

This code is also available in: Deutsch Español Français
Awaiting validation
Detailed functional analysis explaining how to read single or multiple SAS© data sets, create data using DATA Step programming statements, read data sets from user-defined libraries with or without a libref, and use data set name lists for operations like copying. All examples are self-contained through the use of simulated data or the SASHELP library.
Data Analysis

Type : CREATION_INTERNE


Examples use generated data (datalines) or SASHELP, with fictitious data created for cases requiring external files to ensure the autonomy of the examples.

1 Code Block
DATA STEP, PROC PRINT
Explanation :
This example reads a SAS data set from the sashelp library and writes the output to the SAS Work library. The SET statement reads the sashelp.shoes data set into the DATA step where it is processed by the WHERE statement. The WHERE statement selects only observations with a value greater than 500,000 for the sales variable. The DATA step then writes the output to the data set specified in the DATA statement (work.shoes).
Copied!
1DATA work.shoes;
2 SET sashelp.shoes;
3 where sales>500000;
4RUN;
5PROC PRINT DATA=shoes; RUN;
2 Code Block
DATA STEP, PROC PRINT Data
Explanation :
You can create data for a SAS data set by generating observations with programming statements rather than by reading data. A DATA step that reads no input goes through a single iteration. Detailed call explanations are: 1. Start the DATA step and create a SAS data set 'Investment'. 2. Calculate a value based on a capital investment of $2,000 and 7% interest each year from 1990 to 2009. Calculate variable values for one observation per DO loop iteration. 3. Write each observation to the Investment data set. 4. Write a note to the SAS log proving that the DATA step iterates only once. 5. Execute the DATA step. 6. To see your output, print the Investment data set with the PRINT procedure. 7. Use the FORMAT statement to write numeric values with dollar signs, commas, and decimals. 8. Execute the PRINT procedure.
Copied!
1DATA investment;
2 begin='01JAN1990'd;
3 END='31DEC2009'd;
4 DO year=year(begin) to year(END);
5 Capital+2000 + .07*(Capital+2000);
6 OUTPUT;
7 END;
8 put 'The number of DATA step iterations is '_n_;
9RUN;
10 
11PROC PRINT DATA=investment;
12 FORMAT Capital dollar12.2;
13RUN;
3 Code Block
DATA STEP, PROC PRINT
Explanation :
This example reads three data sets from the Sashelp library and then concatenates them into a single output data set named concat. Since no SAS library or output location is specified, the output data set, concat, is temporarily saved in the SAS Work library. The output data set consists of observations from all three data sets. The order in which the data sets are concatenated into the output data set is based on how the data sets are listed in the SET statement. Observations from sashelp.nvst1 are first, followed by observations from sashelp.nvst2, followed by observations from sashelp.nvst3.
Copied!
1DATA concat;
2 SET sashelp.nvst1 sashelp.nvst2 sashelp.nvst3;
3RUN;
4PROC PRINT DATA=concat; RUN;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.