Examples use existing SASHELP datasets (Sashelp.Cars, Sashelp.Class, Sashelp.Comet, Sashelp.Quakes) or simple dataset creations based on them.
1 Code Block
DATA STEP
Explanation : This example uses the KEEP statement to control which variables are written to the output dataset. The LIBNAME statement specifies the location for writing the output dataset and associates it with the name mysas. The SET statement reads the input dataset, Sashelp.Cars, and the DATA statement writes the results to the output dataset mysas.cars. This example uses the KEEP statement to include only the Make, Mpg, and MSRP variables in the output dataset. SAS reads all variables from the Sashelp.Cars dataset into memory, then drops unwanted variables when creating the output dataset. The MPG_City and MPG_Highway variables are read into memory by the DATA step and are used to calculate the weighted average of fuel consumption, but they are not included in the output.
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
Explanation : This example uses the WHERE statement to select observations based on the values of the age and height variables. When the DATA step executes this program, it does not read every row of the input dataset. Using the WHERE statement can improve the efficiency of your SAS program when the DATA step needs to read fewer observations. You can also use the WHERE= dataset option to conditionally select observations in the input or output dataset.
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 Data
Explanation : In this example, the DATA step directly accesses a specific observation in the input dataset and writes the output to a new dataset starting with that specified observation. The FIRSTOBS= dataset option specifies that observations are written to the output dataset, quakes2, starting from observation number 5 of the input dataset, Sashelp.quakes. To create an input dataset for this example, the following DATA step is used to create a subset of the Sashelp.Quakes dataset. The DATA step creates a subset by conditionally selecting observations where the Magnitude variable values are greater than 6.0. In the next DATA step, the FIRSTOBS= dataset option is specified in the SET statement to create a new output dataset that starts with observation 5 of the input dataset. The output dataset contains all remaining observations from the input dataset.
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 Data
Explanation : In this example, the DATA step accesses a range of observations in a dataset by using the FIRSTOBS= and OBS= dataset options together. The FIRSTOBS= dataset option specifies that observations are written to the output dataset, quakes2, starting from observation number 2 of the input dataset, Sashelp.quakes. The OBS= dataset option tells SAS to stop processing observations after reading a specified number of observations. SAS uses the following formula to determine the number of observations to read when using the OBS= and FIRSTOBS= dataset options together: (obs - firstobs) + 1 = number of rows. To create the input dataset for this example, the first DATA step creates a subset of the Sashelp.Quakes dataset. The second DATA step creates a range of observations.
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
Explanation : In this example, the DATA step directly accesses the third row of the Sashelp.Comet dataset. A temporary numeric variable, num, is created to hold the value of the observation to be directly accessed from the Sashelp.Comet dataset. The assignment statement creates the variable and assigns it the value 3, which represents the third observation of the Sashelp.Comet dataset. Then, the POINT= option is specified in the SET statement and is set equal to the temporary variable num. The OUTPUT statement writes the current observation to the output dataset Comet. The STOP statement is used to prevent continuous processing of the DATA step. The CALL SYMPUT routine assigns the row number to a macro variable that can be used in the TITLE statement to indicate the row being accessed. The first PROC PRINT below is used to display the first few rows of the input dataset from which the third row is directly accessed by the DATA step.
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.