Published on :
Macro SASHELP

Data Dictionary Generation (CodeBook) via Macro

This code is also available in: Deutsch Español Français
Awaiting validation
This 'MakeCodeBook' macro automates the documentation of SAS© tables. It uses PROC CONTENTS to retrieve the list of variables and their attributes, captures the ODS 'Variables' output into a table, sorts the results by the logical order of variables (num), and exports the final result to a CSV file at the specified location.
Data Analysis

Type : SASHELP


By default, the macro targets the 'sashelp.class' table, but it can be redirected to any table via the 'dslib' and 'dsname' parameters.

1 Code Block
PROC CONTENTS Data
Explanation :
Macro initialization and metadata extraction. The 'ods output Variables=CodeBook' statement captures the PROC CONTENTS output table containing the list of variables into the temporary SAS table 'CodeBook'.
Copied!
1%macro MakeCodeBook(dslib=sashelp,dsname=class,outdir=);
2 ods OUTPUT Variables=CodeBook;
3 PROC CONTENTS DATA=&dslib..&dsname;
4 RUN;
2 Code Block
PROC SORT
Explanation :
Sorting the 'CodeBook' metadata table by the 'num' variable, which represents the physical order of variables in the original table.
Copied!
1 PROC SORT DATA=CodeBook;
2 BY num;
3 RUN;
3 Code Block
PROC EXPORT
Explanation :
Exporting the sorted metadata table to an external CSV file. The output path is dynamically constructed using macro parameters.
Copied!
1 PROC EXPORT DATA=CodeBook
2 outfile="&outdir/&dsname._CodeBook.csv"
3 dbms=csv replace;
4 RUN;
5%mend MakeCodeBook;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.