Published on :
Macro CREATION_INTERNE

Listvar Macro: Extracting and Sorting Variable Metadata

This code is also available in: Deutsch Español Français
Awaiting validation
This script defines the `%listvar` macro which accepts a dataset name as a parameter. It uses the `CONTENTS` procedure to extract metadata (name, variable number, table name, label) into an output table (named after the input table with the suffix 'names'). Then, it sorts this results table by the variable creation order (`varnum`).
Data Analysis

Type : CREATION_INTERNE


The script is a macro definition that generates a metadata table based on the table name passed as an argument.

1 Code Block
MACRO Data
Explanation :
Definition of the `listvar` macro. It executes `PROC CONTENTS` to generate an output table containing the names and positions of the variables from the `&dataset` table, then sorts this resulting table by the `varnum` variable.
Copied!
1%macro listvar(dataset);
2 
3 PROC CONTENTS DATA=&dataset. out=&dataset.names(keep=name varnum memname label) noprint;
4 RUN;
5 
6 PROC SORT DATA=&dataset.names;
7 BY varnum;
8 RUN;
9 
10%mend;
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.
Copyright Info : adopted from https://chemicalstatistician.wordpress.com/2015/01/06/get-a-list-of-the-variable-names-of-a-sas-data-set/