Open In App

How to drop variables from a dataset in SAS Programming?

Improve
Improve
Like Article
Like
Save
Share
Report

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 20
13 F 6 21 25 17
19 F 9 19 12 15
10 M 12 7 21 25
25 F 10 15 22 13
13 F 11 20 22 27
;
proc print;
run;


Output:

The main differences between the two are as follows:

  1. 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 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.

  2. 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;

    
    

    or




    data readin;
    set outdata;
    drop class;
    if gender = 'F';
    run;

    
    

    DROP = option




    data readin;
    set outdata (drop = class);
    if  gender = 'F';
    run;

    
    

    Output:

  3. Scenario: Dropping variables while printing
    DROP 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:



Last Updated : 23 Jul, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads