This post explains how to drop variables from a dataset in SAS. It includes various tricks to delete variables from data.In SAS, there are two ways to handle dropping variables :
III. Scenario : Dropping variables while printing
DROP statement can be used in DATA steps only whereas DROP = option can be used in DATA steps and PROC steps (for printing)
- DROP = data set option
- DROP statement
Let's start with creating a data set :
The main differences between the two are as follows :
I. Scenario : Create a new variable based on existing data and then drops the irrelevant variables
By using the DROP statement, we can command SAS to drop variables only at completion of the DATA step.
Consequence of using DROP = Option
data readin;
set outdata (drop = obs1 obs2 obs3);
totalsum = sum(obs1,obs2,obs3);
run;
The variables obs1,obs2 and obs3 are not available for use after data set outdata has been copied into the new data set readin . Hence totalsum would contain missing values only.
II. DROP statement can be used anywhere in DATA steps whereas DROP = option must follow the SET statement.
By using the DROP statement, we can command SAS to drop variables only at completion of the DATA step.
data readin;In the above example, we simply ask SAS sum up all the values in variables obs1,obs2 and obs3 to produce a new variable totalsum and then drop the old variables obs1,obs2 and obs3.
set outdata;
totalsum = sum(obs1,obs2,obs3);
drop obs1 obs2 obs3;
run;
Consequence of using DROP = Option
data readin;
set outdata (drop = obs1 obs2 obs3);
totalsum = sum(obs1,obs2,obs3);
run;
The variables obs1,obs2 and obs3 are not available for use after data set outdata has been copied into the new data set readin . Hence totalsum would contain missing values only.
II. DROP statement can be used anywhere in DATA steps whereas DROP = option must follow the SET statement.
DROP statement
data readin;
set outdata;
if gender = 'F';
drop age;
run;
OR
data readin;
set outdata;
drop age;
if gender = 'F';
run;
DROP = option
data readin;
set outdata (drop = age);
if gender = 'F';
run;
III. Scenario : Dropping variables while printing
DROP statement can be used in DATA steps only whereas DROP = option can be used in DATA steps and PROC steps (for printing)
proc print data = outdata (drop = age);
where gender = 'F';
run;
DROP = option
ReplyDeletedata readin;
set outdata (drop = age height);
if gender = 'F';
run;
if it will drop age and height first then how the if condition will be satisfied ? little confused.Will appreciate if you help.
it will not drop the vaiables age and height when the data statement is run but it will be droped while writing to the data set in the pdv.
Deleteand if ur data contained a attribute as gender then u can drop other variable for using any conditional statement .\
we command to SAS print data from outdata and drop age height where gender (F)
ReplyDeletei dint c any condition will be not satisfied
In that case age and height of only females will come.
ReplyDeleteReally Helpfull
ReplyDeletehello, does keep statement and option act exactly like drop counterpart?
ReplyDeletethanks
Hi,
ReplyDeleteJust one correction. Created data set does not contain any height variable.
Please update accordingly.
Thanks for your great work.
Thanks for pointing it out. Corrected!
DeleteHi,
ReplyDeleteWe can use drop statement in data step also right?
data readin (drop = obs1 obs2 obs3);
set outdata ;
totalsum = sum(obs1,obs2,obs3);
run;
Yes! That's right👍
DeleteThanks for detailed tutorial
ReplyDeleteReally helpful
I am getting an error after running below mention code.. Pls advise anyone
ReplyDeleteProc print data = mylib. import (Keep=Region Plan Actual) (obs=10);
Run;
As per my knowledge, DROP option can be used after data statement too.
ReplyDeleteAfter droping a variable from a dataset can we alter those variable?
ReplyDeletetotalsum = sum(obs1,obs2,obs3);
drop obs1 obs2 obs3;
/*Now is this possible!*/newsum=2*totalsum
yes,
Deletetotalsum = sum(obs1,obs2,obs3);
drop obs1 obs2 obs3;
newsum=2*totalsum;
As you can see that here drop is used as data statement so pdv is generated for those variable, so we can modify or delete those variable.
confused please help out.
ReplyDeleteIn abv we are selecting gender.. so why we are dropping age?
and age to khai mntn ni h.
Hello sir,
ReplyDeleteamazing free tutorials for SAS. Helping me a-lot in learning.
I have 2 issues:
1. unable to download the University edition
2. Can't access the file "Advanced SAS Interview Questions and Answers".
Will be of great help if you can help me with these two things sir.
Thank You!
Keep up the great work sir.
What exactly issue you are facing in downloading the university edition? I fixed the second one - you should access the file now. Please check and confirm the same. Thanks!
Delete