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!
libname mysas "c:\Users\demo";
data cars;
set sashelp.cars;
keep make mpg MSRP;
Mpg=(MPG_City*.45)+(MPG_Highway*.55)/2;
run;
proc print data=cars(obs=10); run;
1
LIBNAME mysas "c:\Users\demo";
2
DATA cars;
3
SET sashelp.cars;
4
keep make mpg MSRP;
5
Mpg=(MPG_City*.45)+(MPG_Highway*.55)/2;
6
RUN;
7
PROC PRINTDATA=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!
data class;
set sashelp.class;
where age>12 and height>=67;
run;
proc print data=class; run;
1
DATA class;
2
SET sashelp.class;
3
where age>12 and height>=67;
4
RUN;
5
PROC PRINTDATA=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!
data quakes;
set sashelp.quakes(where=(Magnitude>6.0));
keep Depth Type Magnitude;
run;
proc print data=quakes; run;
data quakes2;
set quakes(firstobs=5);
run;
proc print data=quakes2; run;
1
DATA quakes;
2
SET sashelp.quakes(where=(Magnitude>6.0));
3
keep Depth Type Magnitude;
4
RUN;
5
PROC PRINTDATA=quakes; RUN;
6
7
DATA quakes2;
8
SET quakes(firstobs=5);
9
RUN;
10
PROC PRINTDATA=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!
data quakes;
set sashelp.quakes(where=(Magnitude>6.0));
keep Depth Type Magnitude;
run;
proc print data=quakes; run;
data quakes2;
set quakes(firstobs=2 obs=4);
run;
proc print data=quakes2; run;
1
DATA quakes;
2
SET sashelp.quakes(where=(Magnitude>6.0));
3
keep Depth Type Magnitude;
4
RUN;
5
PROC PRINTDATA=quakes; RUN;
6
7
DATA quakes2;
8
SET quakes(firstobs=2 obs=4);
9
RUN;
10
PROC PRINTDATA=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!
proc print data=sashelp.comet(obs=5);
title "Sashelp.Comet Data Set";
run;
data comet;
num=3;
set sashelp.comet point=num;
call symput('num',num);
output;
stop;
run;
proc print data=comet;
title "Row &num from Sashelp.Comet Data Set";
run;
1
PROC PRINTDATA=sashelp.comet(obs=5);
2
title "Sashelp.Comet Data Set";
3
4
5
RUN;
6
7
DATA comet;
8
num=3;
9
SET sashelp.comet point=num;
10
call symput('num',num);
11
OUTPUT;
12
stop;
13
RUN;
14
15
PROC PRINTDATA=comet;
16
title "Row &num from Sashelp.Comet Data Set";
17
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.
SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. ® indicates USA registration. WeAreCAS is an independent community site and is not affiliated with SAS Institute Inc.
This site uses technical and analytical cookies to improve your experience.
Read more.