April 15, 2017 Leave a comment
After reading my blog post on getting the variable names of a SAS data set, a reader named Robin asked how to get the formats as well. I asked SAS Technical Support for help, and a consultant named Jerry Leonard provided a beautiful solution using PROC SQL. Besides the names and formats of the variables, it also gives the types, lengths, and labels. Here is an example of how to do so with the CLASS data set in the built-in SASHELP library.
* add formats and labels to 3 of the variables in the CLASS data set; data class; set sashelp.class; format age 8. weight height 8.2 name $15.; label age = 'Age' weight = 'Weight' height = 'Height'; run; * extract the variable information using PROC SQL; proc sql noprint; create table class_info as select libname as library, memname as data_set, name as variable_name, type, length, format, label from dictionary.columns where libname = 'WORK' and memname = 'CLASS'; /* libname and memname values must be upper case */ quit; * print the resulting table; proc print data = class_info; run;
Here is the result of that PROC PRINT step in the Results Viewer. Notice that it also has the type, length, format, and label of each variable.
Thank you, Jerry, for sharing your tip!