How to drop variables from a dataset in SAS Programming?

23 Jul, 2019
This topic is regarding how to drop variables from a dataset in SAS. It includes various methods to delete variables from data. In SAS, there are two ways to drop variables:
DROP = data set option
DROP statement
Let’s start with creating a data set:
DATA outdata;    INPUT roll_num gender $ class subj1 subj2 subj3;    DATALINES; 21 F 6 10 17 2013 F 6 21 25 1719 F 9 19 12 1510 M 12 7 21 2525 F 10 15 22 1313 F 11 20 22 27;proc print;run;
Output:
The main differences between the two are as follows:
Scenario: Create a new variable based on existing data and then drops the irrelevant variablesBy using the DROP statement, we can command SAS to drop variables only when DATA step will be completed.data readin;set outdata;totalsum = sum(subj1, subj2, subj3);drop subj1 subj2 subj3;run;Output:In the above example, we simply ask SAS sum up all the values in variables subj1, subj2 and subj3 to produce a new variable totalsum and then drop the old variables subj1, subj2 and subj3.Consequence of using DROP = Optiondata readin;set outdata (drop = subj1 subj2 subj3);totalsum = sum(subj1, subj2, subj3);run;Output:The variables subj1, subj2 and subj3 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.DROP statement can be used anywhere in the DATA steps whereas DROP = option must have to follow the SET statement.DROP statement:data readin;set outdata;if gender = 'F';drop class;run;ordata readin;set outdata;drop class;if gender = 'F';run;DROP = optiondata readin;set outdata (drop = class);if  gender = 'F';run;Output:Scenario: Dropping variables while printingDROP statement can be used in DATA steps only whereas DROP = option can be used in both DATA steps and PROC steps (for displaying purpose).proc print data = outdata (drop = class);where gender = 'F';run;Output:
Scenario: Create a new variable based on existing data and then drops the irrelevant variablesBy using the DROP statement, we can command SAS to drop variables only when DATA step will be completed.data readin;set outdata;totalsum = sum(subj1, subj2, subj3);drop subj1 subj2 subj3;run;Output:In the above example, we simply ask SAS sum up all the values in variables subj1, subj2 and subj3 to produce a new variable totalsum and then drop the old variables subj1, subj2 and subj3.Consequence of using DROP = Optiondata readin;set outdata (drop = subj1 subj2 subj3);totalsum = sum(subj1, subj2, subj3);run;Output:The variables subj1, subj2 and subj3 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.
By using the DROP statement, we can command SAS to drop variables only when DATA step will be completed.
data readin;set outdata;totalsum = sum(subj1, subj2, subj3);drop subj1 subj2 subj3;run;
Output:
In the above example, we simply ask SAS sum up all the values in variables subj1, subj2 and subj3 to produce a new variable totalsum and then drop the old variables subj1, subj2 and subj3.
Consequence of using DROP = Option
data readin;set outdata (drop = subj1 subj2 subj3);totalsum = sum(subj1, subj2, subj3);run;
Output:
The variables subj1, subj2 and subj3 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.
DROP statement can be used anywhere in the DATA steps whereas DROP = option must have to follow the SET statement.DROP statement:data readin;set outdata;if gender = 'F';drop class;run;ordata readin;set outdata;drop class;if gender = 'F';run;DROP = optiondata readin;set outdata (drop = class);if  gender = 'F';run;Output:
DROP statement:
data readin;set outdata;if gender = 'F';drop class;run;
or
data readin;set outdata;drop class;if gender = 'F';run;
DROP = option
data readin;set outdata (drop = class);if  gender = 'F';run;
Output:
Scenario: Dropping variables while printingDROP statement can be used in DATA steps only whereas DROP = option can be used in both DATA steps and PROC steps (for displaying purpose).proc print data = outdata (drop = class);where gender = 'F';run;Output:
proc print data = outdata (drop = class);where gender = 'F';run;
Output:
SAS Programming
Programming Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
return 0 vs return 1 in C++
Difference between while and do-while loop in C, C++, Java
C# | Data Types
Prolog | An Introduction
Shallow Copy and Deep Copy in C++
Advantages and Disadvantages of OOP
Kotlin Array
Learn C++ Programming Step by Step - A 20 Day Curriculum!
Use of FLAG in programming
Difference between Shallow and Deep copy of a class