Return to Archive

SAS Tip of the Month
May 2010

This month is a quick tip about appending data -- making a SAS datastep do what a APPEND statement or procedure call does.

First lets have a look at some data:

   data x;
      length a b d 8;
      call missing(of _all_);
   data y;
      length a b c d 8;
      input a b c d;
   1 2 3 5
   2 3 6 4

The usual way of appending the data in dataset y to dataset x is to do the following:

   proc datasets library=work nolist nodetails nowarn;
      append base=x data=y force;

But as I mentioned above, we do not want to use the APPEND statement so is there another way? Could be use the following:

   data x;
      set x y;

Using a datastep, the SET statement will not quite do it as the variables in datasets x and y are added to x, resulting in dataset x having variable c that is not in the initial datastep.

What must be considered is using a statement that will take the structure of dataset x, and then get the records of dataset y and apply them to dataset x without changing the structure, as the following will do:

   data x;
      if 0 then modify x;
      set y;

This has the effect of calling the dataset x in modify mode (therefore variables cannot be created in dataset x) and bringing in the data from y. The output statement outputs the record to dataset x.

Coming up later this month I will be presenting two papers at PharmaSUG in Orlando -- one will be a presentation doing a countdown of the Top Ten ways to Merge data, and a second is presenting a small macro that will turn an ASCII Text output to an RTF output inside a single datastep, with the use of ODS! If you are there, please come and say 'hi'.

Updated May 03, 2010