Return to Archive

SAS Tip of the Month
March 2010

The DATASETS procedure has been around for a long time. Its main purpose is to manage your SAS datasets with tasks that include copying, deleting and renaming datasets, renaming variables inside a dataset, and adding formats and labels to a dataset. The procedure can also manage indexes inside a dataset. But one thing that is not clear is how to drop or keep variables in a dataset.

There is a little trick that is useful when you want to DROP or KEEP variables in a dataset -- use the APPEND statement, as the following statement shows:


      ** Copy in the CLASS dataset from SASHELP;
      COPY IN=sashelp OUT=work;
         SELECT class;

      ** DROP variable AGE from the dataset;
      APPEND BASE=class0 DATA=class (DROP=age);


In this example we have made a copy of the dataset CLASS and created CLASS0, on the way we have also dropped the variable AGE. Note that the BASE and DATA options in the APPEND statement are different -- you cannot have these two parameters with the same name (I personally believe this is a nice feature as it forces the programmer to create a new dataset with each step of a program, and makes the task of reading the program by another person easier). It also must be noted that the dataset CLASS0 must not exist -- if it does then any data in CLASS will be appended to CLASS0.

At first thought the COPY statement should do this task but this statement will not allow you to run the normal dataset options like the DROP and KEEP statements, only the APPEND procedure will allow you to this.

Hope you have a happy and safe March -- see you in April.

Updated March 02, 2010