View the Contents of a Format Library

* View the contents of an format library;
proc catalog catalog = febstat.formats;
contents;
run;
quit;

* Examine the values of specific format in the library;
proc format library = febstat.formats;
select gender;
run;
quit;

*To print out a saved FORMAT library, use the FMTLIB option as indicated:
libname library;
proc format library=library fmtlib;
run;

Download SAS Program

Dynamic Formatting in SAS

I recently needed to color format some observations in a report based on information stored in multiple tables. Essentially these records had to meet certain criteria within a given bygroup. This macro dynamically creates the format statement needed to properly render the report. By avoiding hard coding the report will properly format the correct observations without my needing to touch the code should the observations that meet my criteria change over time.

%macro dynamicFormat(byvar=);
%do byvar=1 %to 5;
proc sql noprint;
create table EXAMPLE as
select distinct a.id,
c.id3 as byvar
from lib.dataset1 a, lib.dataset2 b, lib.dataset3 c
where a.id = b.id and
c.id2 = a.id2 and
a.var1 = 1 and
a.var2 = 1 and
c.id3 eq "&byvar.";
select e.var1 into :uni&byvar._highlightBG separated by ' = "Black" '
from EXAMPLE d, lib.dataset5 e
where d.id = e.id;
select e.var1 into :uni&byvar._highlightFG separated by ' = "White" '
from EXAMPLE d, lib.dataset5 e
where d.id = e.id;
quit;
%end;
%let uni1_highlightBG2 = &uni1_highlightBG. = "Black";%let uni1_highlightFG2 = &uni1_highlightFG. = "White";
%let uni2_highlightBG2 = &uni2_highlightBG. = "Black";%let uni2_highlightFG2 = &uni2_highlightFG. = "White";
%let uni3_highlightBG2 = &uni3_highlightBG. = "Black";%let uni3_highlightFG2 = &uni3_highlightFG. = "White";
%let uni4_highlightBG2 = &uni4_highlightBG. = "Black";%let uni4_highlightFG2 = &uni4_highlightFG. = "White";
%let uni5_highlightBG2 = &uni5_highlightBG. = "Black";%let uni5_highlightFG2 = &uni5_highlightFG. = "White";
proc format;
value $uni1_highlightBG &uni1_highlightBG2.;
value $uni1_highlightFG &uni1_highlightFG2.;
value $uni2_highlightBG &uni2_highlightBG2.;
value $uni2_highlightFG &uni2_highlightFG2.;
value $uni3_highlightBG &uni3_highlightBG2.;
value $uni3_highlightFG &uni3_highlightFG2.;
value $uni4_highlightBG &uni4_highlightBG2.;
value $uni4_highlightFG &auni4_highlightFG2.;
value $uni5_highlightBG &uni5_highlightBG2.;
value $uni5_highlightFG &uni5_highlightFG2.;
run;
%mend dynamicFormat;
%dynamicFormat;

format libraries as lookup tables

This is some debugging information regarding a program written to using a format library as a lookup table. Sorry but it may not make sense without all the details!
In this case the statement to create the control value based on the a key’s format was as follows:

control=put(key,control.);

However, when SAS can’t find a key value the [format] lookup table it will define the control variable using the initial value of the variable key.
Proc format defines a default length to be the longest value it finds in the dataset when it uses the CNTLIN option. If the longest value SAS initially discovers for the control variable has a length of 6 SAS will thus round off any future values that are greater than a length of 6 (e.g. 12345678 would become 12345600). Setting a larger value length by adding "default 12" to the retain statement will fix this.

libname library '/path/to/sasdata';
libname sasdata '/path/to/sasdata';
data ctrl;
set sasdata.control(rename=(key=start control=label));
retain fmtname 'control' type 'n' default 12;
output;
run;

proc format library=library cntlin=ctrl fmtlib;
run;