Published on :

Dynamic Column Display with PROC SQL and PROC PRINT

This code is also available in: Deutsch Español Français
The first step of the script uses `PROC SQL` to query the `dictionary.columns` metadata table. It selects all column names from the `SASUSER.HOUSES` dataset and stores them in a macro variable named `varlist`, with each column name separated by a space. The second step calls `PROC PRINT` to display the content of the `SASUSER.HOUSES` dataset. Using the `VAR &varlist` clause allows only the columns whose names have been dynamically retrieved and stored in the macro variable to be displayed, thus ensuring a customizable display of the data.
Data Analysis

Type : MIXED


The script queries the `dictionary.columns` system table to obtain metadata about the columns. It then uses the `SASUSER.HOUSES` dataset, which is a typical dataset in the SAS environment, often available by default or through examples.

1 Code Block
PROC SQL
Explanation :
This block uses `PROC SQL` in `noprint` mode to query the `dictionary.columns` system view. It selects all column names from the `SASUSER.HOUSES` dataset and concatenates them into the `varlist` macro variable, each name separated by a space, for later use in the script.
Copied!
1PROC SQL noprint;
2 select name into :varlist separated BY " "
3 from dictionary.columns
4 where upcase(LIBNAME)="SASUSER" and upcase(memname)="HOUSES"
5 order BY name;
6QUIT;
2 Code Block
PROC PRINT
Explanation :
This block executes `PROC PRINT` to display data from the `sasuser.houses` dataset. The `VAR &varlist;` clause uses the `varlist` macro variable created previously to dynamically specify the columns to display, thereby allowing only the selected columns to be viewed.
Copied!
1 
2PROC PRINT
3DATA=sasuser.houses ;
4var &varlist;
5RUN;
6 
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.