Published on :
Data Manipulation INTERNAL_CREATION

Examples: Controlling Variables and Observations in Data Sets

This code is also available in: Deutsch Español Français
Detailed functional analysis explaining the use of the KEEP statement to retain specific variables, the WHERE statement to filter observations according to defined criteria, and the FIRSTOBS=, OBS= and POINT= options to control the start, end, or direct access to specific observations in a data set.
Data Analysis

Type : INTERNAL_CREATION


Examples use SASHELP data sets (sashelp.cars, sashelp.class, sashelp.quakes, sashelp.comet) or internal data creations, making the examples self-contained.

1 Code Block
DATA STEP / PROC PRINT
Explanation :
This example uses the KEEP statement to specify that only the 'Make', 'Mpg', and 'MSRP' variables should be included in the output 'cars' data set. A new 'Mpg' variable is calculated as a weighted average of 'MPG_City' and 'MPG_Highway'. The 'mysas.cars' data set is then printed, limited to the first 10 observations.
Copied!
1LIBNAME mysas "c:\Users\demo";
2DATA cars;
3 SET sashelp.cars;
4 keep make mpg MSRP;
5 Mpg=(MPG_City*.45)+(MPG_Highway*.55)/2;
6RUN;
7PROC PRINT DATA=cars(obs=10); RUN;
2 Code Block
DATA STEP / PROC PRINT
Explanation :
This example uses the WHERE statement to select observations from the 'sashelp.class' data set where the 'age' value is greater than 12 and the 'height' value is greater than or equal to 67. Only observations meeting these criteria are read and written to the 'class' data set, thus improving program efficiency. An alternative WHERE= option can be used directly in the SET data set option.
Copied!
1DATA class;
2 SET sashelp.class;
3 where age>12 and height>=67;
4RUN;
5PROC PRINT DATA=class; RUN;
3 Code Block
DATA STEP / PROC PRINT
Explanation :
This example first creates a subset of the 'sashelp.quakes' data set where 'Magnitude' is greater than 6.0. Then, the FIRSTOBS=5 option is used in the SET statement to indicate that processing and writing to the new 'quakes2' data set should start from the fifth observation of the 'quakes' data set. The remaining observations from the input data set are included.
Copied!
1DATA quakes;
2 SET sashelp.quakes(where=(Magnitude>6.0));
3 keep Depth Type Magnitude;
4RUN;
5PROC PRINT DATA=quakes; RUN;
6 
7DATA quakes2;
8 SET quakes(firstobs=5);
9RUN;
10PROC PRINT DATA=quakes2; RUN;
4 Code Block
DATA STEP / PROC PRINT
Explanation :
This example illustrates the combined use of the FIRSTOBS= and OBS= options to read a specific range of observations. After creating a 'quakes' subset, the second DATA step reads the 'quakes' data set starting from the second observation (FIRSTOBS=2) and stopping after the fourth observation (OBS=4). This generates an output data set 'quakes2' containing observations 2, 3, and 4.
Copied!
1DATA quakes;
2 SET sashelp.quakes(where=(Magnitude>6.0));
3 keep Depth Type Magnitude;
4RUN;
5PROC PRINT DATA=quakes; RUN;
6 
7DATA quakes2;
8 SET quakes(firstobs=2 obs=4);
9RUN;
10PROC PRINT DATA=quakes2; RUN;
5 Code Block
DATA STEP / PROC PRINT
Explanation :
This example uses the POINT= option in the SET statement to directly access a specific observation (the third) from the 'sashelp.comet' data set. A temporary variable 'num' is assigned to 3, and POINT=num tells SAS to access this observation. The OUTPUT statement writes the observation, and STOP is used to prevent continuous processing of the DATA step, as POINT= does not detect end-of-file. A TITLE is dynamically generated.
Copied!
1PROC PRINT DATA=sashelp.comet(obs=5);
2title "Sashelp.Comet Data Set";
3 
4 
5RUN;
6 
7DATA comet;
8 num=3;
9 SET sashelp.comet point=num;
10 call symput('num',num);
11 OUTPUT;
12 stop;
13RUN;
14 
15PROC PRINT DATA=comet;
16title "Row &num from Sashelp.Comet Data Set";
17RUN;
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.
Copyright Info : Copyright © SAS Institute Inc. All Rights Reserved