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:
create table work.cars_table
…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.
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:
create table work.cars_table(drop=Type Origin DriveTrain)
…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.
…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!
(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.