How to drop variables from a dataset in SAS Programming?

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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;

chevron_right


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.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    data readin;
    set outdata;
    totalsum = sum(subj1, subj2, subj3);
    drop subj1 subj2 subj3;
    run;

    chevron_right

    
    

    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

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    data readin;
    set outdata (drop = subj1 subj2 subj3);
    totalsum = sum(subj1, subj2, subj3);
    run;

    chevron_right

    
    

    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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    or

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    DROP = option

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

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

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    proc print data = outdata (drop = class);
    where gender = 'F';
    run;

    chevron_right

    
    

    Output:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.