SAS : Keep Statement & Drop
Share
KEEP
When we want to retrieve only few variables in SAS dataset , we used Keep .
The KEEP= option tells SAS which variable you want to keep in a data set . If you place the KEEP= option on the SET statement , SAS keeps the specified variables when it reads the input data set. If you place the KEEP= option on the DATA statement , SAS keeps the specified variables when it writes to the output data set.
The KEEP= option must be placed in parentheses and follow the name of the data set from you which you want SAS to select variables. The list of variables that you specify to keep must be separated by at least one blank space.
We create a temporary data set “abc” as :
data abc;
input Id Name $ Salary Age;
datalines;
101 Denny 12457 45
102 Gary 57846 78
103 Fred 78451 40
;
run;
We used KEEP= option in SET statement . The SET statement’s KEEP= option tell SAS to read Id variable from the temporary data set abc to store it in a new data set new.
data new;
set abc(keep=Id);
run;
The DATA statement’s KEEP= option is used to tell SAS to write Id variable to the new data set.
data new(keep=Id);
set abc;
run;
We can also Keep multiple variables in SET statement as :
data new;
set abc(keep= id name);
run;
Output:
We can also Keep multiple variables in DATA statement as :
data new(keep= id name);
set abc;
run;
Output:
We can use KEEP= option to keep character variable .
data new(keep = _character_) ;
set abc;
run;
KEEP statement
We can also used KEEP statement to keep variables which you want to keep in a data set.
The syntax of KEEP statement is :
KEEP variable_name1 variable_name2 ;
The following program keep Id and Name variables to new data set .
data new;
set abc;
keep id name;
run;
Output:
We can keep numeric variables by using KEEP statement as :
data new ;
set abc;
keep _numeric_;
run;
Output:
We can keep character variables by using KEEP statement as :
data new ;
set abc;
keep _character_;
run;
DROP
The DROP is used to tells SAS which variables you want to drop from a data set . If you place the DROP= option on the SET statement, SAS drops the specified variables when it reads the input data set. On the other hand, if you place the DROP= option on the DATA statement, SAS drops the specified variables when it writes to the output data set.
When we use DROP= option in DATA statement to tell SAS to drop Id variable when writing to the output data set new.
data new(drop=Id);
set abc;
run;
When we used DROP= option in SET statement to tell SAS to drop Id variable from the temporary data set abc and to store the remaining variables in an new temporary data set new.
data new;
set abc(drop=Id);
run;
Output:
We can also drop multiple variables by using DROP= option in DATA statement as :
data new(drop=Id salary);
set abc;
run;
Output:
We can also drop multiple variables by using DROP= option in SET statement as :
data new;
set abc(drop=Id salary);
run;
Output:
We can used DROP statement to drop variables .We drop id and name variables by using DROP statement as:
data new;
set abc;
drop id name;
run;
Output:
We can used DROP statement to drop numeric variables .
data new ;
set abc;
drop _character_;
run;
Output:
We create a “new” data set as :
data new ;
input id name$ salary;
datalines;
101 Denny 1256
102 Ram 1587
103 Shyam 4578
104 Hari 6658
105 Radha 7845
106 Jay 9874
107 Hema 7865
108 Kemu 6584
109 Tapi 2546
110 Parul 5465
;
run;
Output :
FIRSTOBS
It is used to begin reading observation from the input data set at the line specified by FIRSTOBS.
data abc ;
set new(firstobs=5);
run;
It shows observations starting from 5th observation .
Output:
We can also stop reading the data from the input data set at the line number specified by OBS.
data abc ;
set new(obs=5);
run;
It shows starting 5 observations of our data set.
Output:
If we want to read only 5th observation , we can use FIRSTOBS= 5 and OBS= 5 .
data internal;
set new(firstobs=5 obs=5);
run;
Output:
We reading data from 6th observation to 9th observation.
data internal;
set new(firstobs=6 obs=9);
run;
Output: