SAS author's tip: Help from the DESCRIBE TABLE statement

6

The first line of this week's SAS tip grabs your attention, "PROC SQL provides a helpful (though potentially dangerous) tool in the form of the DESCRIBE TABLE statement." SAS author, consultant, and member of the SAS-L Hall of Fame Howard Schreier included this intriguing statement in his book PROC SQL by Example: Using SQL within SAS--and then clarified what he meant. If you flip through Howard's book, you'll find an abundance of programming tips, examples, and glowing reviews from SAS users. You can also read these reviews and a free chapter on his author page.

The following excerpt is from SAS author Howard Schreier and his book "PROC SQL by Example: Using SQL within SAS" Copyright © 2008, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED. (please note that results may vary depending on your version of SAS software)

Help from the DESCRIBE TABLE Statement

PROC SQL provides a helpful (though potentially dangerous) tool in the form of the DESCRIBE TABLE statement. It examines an existing table, then displays (in the log) the CREATE TABLE statement that would create that table in an empty state (that is, with no rows of data). For example, if we run:

PROC SQL;
DESCRIBE TABLE demolib.fifteenups;
QUIT;

we get, in the log:You can copy this code from the log and use it in your source code. Of course, if you do that without making any changes, and if you do not have the NOREPLACE system option or some other safety mechanism in effect, you can easily destroy the model table (the one referenced by the DESCRIBE TABLE statement) and replace it with an empty clone. So it’s more likely that you will want to edit the code before running it and, in particular, change the name or library location of the new table.

The DESCRIBE TABLE statement is useful in a variety of situations. If you want to code a self-contained CREATE TABLE statement for a table that is to be almost but not exactly like an existing table, you can use DESCRIBE TABLE to generate a first approximation of your CREATE TABLE statement. You can then edit the statement. If you want to pattern a new table on an existing table and then “freeze” that borrowed structure so that your code is insulated from later changes to the model table, you cannot use the CREATE TABLE statement with the LIKE keyword. Instead, use DESCRIBE TABLE to generate the code you need. If you prefer to design a table using the DATA step and then translate the specifications into SQL, use DESCRIBE TABLE to produce the CREATE TABLE statement.

 

Share

About Author

Shelly Goodin

Social Media Specialist, SAS Publications

Shelly Goodin is SAS Publications' social media marketer and the editor of "SAS Publishing News". She’s worked in the publishing industry for over thirteen years, including seven years at SAS, and enjoys creating opportunities for fans of SAS and JMP software to get to know SAS Publications' many offerings and authors.

6 Comments

    • Howard Schreier on

      This is simplest way to propagate a table structure. It has advantages and disadvantages vis a vis the technique based on the DESCRIBE statement; see the longest paragraph of the original post.

  1. Quentin McMullen on

    Just wanted to say that this is a great book. I have spent more of my SAS career in data step rather than SQL. The examples in this book, comparing data step methods to SQL analogs, will be very helpful for anyone who knows SAS, and wants to do more in SQL.

  2. Divyesh Dave on

    The following code can also be handy if we want to create a preliminary version of a new table :

    proc sql;
    create table newTable as select * from demolib.fifteenups;
    quit;

    • Howard Schreier on

      Another variation is to use the OBS=0 data set option to reproduce the structure without the content (rows):

      create table newTable as select * from demolib.fifteenups(obs=0) ;

Back to Top