Monday, May 9, 2016

Hack 3.6 Creating Empty SAS Data Sets with Same Attributes As Existing Ones


SAS Programming Professionals, 

Did you know that you can easily create a new, empty SAS data set with exactly the same attributes as an existing one? 

The LIKE clause allows you to do this in a simple PROC SQL step:
      
      proc sql;
      create table work.cars_table
            like sashelp.cars;
quit;

 …which produces the following log entry: 

1    proc sql;
2        create table work.cars_table
3            like sashelp.cars;
NOTE: Table WORK.CARS_TABLE created, with 0 rows and 15 columns.
4    quit;

Now, we can use PROC APPEND or PROC SQL or… whatever to populate the new cars_table SAS data set.

If you do not want all of the variables in the old SAS data set to be created in the new one, you can use DROP or KEEP statements to screen them out.  Here is an example: 

proc sql;
      create table work.cars_table(drop=Type Origin DriveTrain)
           like sashelp.cars;
quit;

…producing this SAS log entry:

8    proc sql;
9        create table work.cars_table(drop=Type Origin DriveTrain)
10           like sashelp.cars;
NOTE: Table WORK.CARS_TABLE created, with 0 rows and 12 columns.
11   quit;

…since we dropped the variables: Type, Origin, and DriveTrain. 

This method of cloning an existing SAS data set can save you time lots of time since you will not have to code variable names, lengths, labels, formats, informats, etc.

Best of luck in all your SAS endeavors!
----MMMMIIIIKKKKEEEE
(aka Michael A. Raithel)

Excerpt from the book:  Did You Know That?  Essential Hacks for Clever SAS Programmers

I plan to post each and every one of the hacks in the book to social media on a weekly basis.  Please pass them along to colleagues who you know would benefit.