LOADING

Type to search

SAS : Keep Statement & Drop

To Know more about the Different Corporate Training & Consulting Visit our website www.Instrovate.com Or Email : info@instrovate.com or WhatsApp / Call at +91 74289 52788

Data Analytics SAS

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;

SAS : Keep Statement & Drop 29

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;

SAS : Keep Statement & Drop 30

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;

SAS : Keep Statement & Drop 31

We can also Keep multiple variables in SET statement as :

data new;

set abc(keep= id name);

run;

Output:

SAS : Keep Statement & Drop 32

We can also Keep multiple variables in DATA statement as :

data new(keep= id name);

set abc;

run;

Output:

SAS : Keep Statement & Drop 33

We can use KEEP= option to keep character variable .

data new(keep = _character_) ;

set abc;

run;

SAS : Keep Statement & Drop 34

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:

SAS : Keep Statement & Drop 35

We can keep numeric variables by using KEEP statement as :

data new ;

set abc;

keep _numeric_;

run;

Output:

SAS : Keep Statement & Drop 36

We can keep character variables by using KEEP statement as :

data new ;

set abc;

keep _character_;

run;

SAS : Keep Statement & Drop 37

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;

SAS : Keep Statement & Drop 38

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:

SAS : Keep Statement & Drop 39

We can also drop multiple variables by using DROP= option in DATA statement as :

data new(drop=Id salary);

set abc;

run;

Output:

SAS : Keep Statement & Drop 40

We can also drop multiple variables by using DROP= option in SET statement as :

data new;

set abc(drop=Id salary);

run;

Output:

SAS : Keep Statement & Drop 41

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:

SAS : Keep Statement & Drop 42

We can used DROP statement to drop numeric variables .

data new ;

set abc;

drop _character_;

run;

Output:

SAS : Keep Statement & Drop 43

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 :

SAS : Keep Statement & Drop 44

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:

SAS : Keep Statement & Drop 45

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:

SAS : Keep Statement & Drop 46

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:

SAS : Keep Statement & Drop 47

We reading data from 6th observation to 9th observation.

data internal;

set new(firstobs=6 obs=9);

run;

Output:

SAS : Keep Statement & Drop 48

Leave a Comment

Your email address will not be published. Required fields are marked *