SAS9

How to copy a table's structure without the data?

Simon 13 Aufrufe

In daily data management with SAS©, it is often necessary to duplicate an existing table. But what if you only want the "skeleton" of the table (the variables, formats, types) without importing the thousands or millions of rows it contains?

Whether you need to create an empty template for new data entry or to initialize an archive table, here is the simplest and most effective method for copying a table structure from one library to another.

Note :
The most elegant method in Base SAS© relies on using the DATA step combined with the STOP statement.
1DATA ma_lib.nouvelle_table;
2 SET autre_lib.ancienne_table;
3 STOP;
4RUN;
Note :
Imagine you have a table VENTES_2023 in the PROD library and you want to create an empty structure VENTES_2024 in your WORK library
1DATA work.ventes_2024;
2 SET prod.ventes_2023;
3 STOP;
4RUN;
Result: You will get a work.ventes_2024 table containing exactly the same columns as the original, but it will be completely empty.

Why does this work?

To understand this trick, you need to look under the hood of the SAS© engine:

  1. The SET statement: At compile time, SAS© reads the descriptor of the source table (prod.ventes_2023). It prepares the "Program Data Vector" (PDV) with all the variables and their attributes. The structure is thus ready.

  2. The STOP statement: Normally, the DATA step works like an implicit loop that reads rows one by one. The STOP statement forces the immediate termination of the DATA step before the first row of data is even loaded into the PDV and written to the output table.

It is this immediate interruption that allows the structure (defined at compilation) to be saved without ever processing the data (execution).

The next time you need an empty table template, remember: a simple STOP is all it takes!