SAS Formats: save and apply

Reference: http://support.sas.com/resources/papers/proceedings12/048-2012.pdf

Save a Format:

Libname sasformat 'c:\sas\format';

Define Catalog:

Proc Format Library = sasformat.format1;

Copy formats in work library catalog to local permanent drive.

Proc catalog catalog=work.formats;
copy out=sasformat.formats;
run;

Check All the Formats in the Catalog:

Proc Catalog Catalog = sasformat.format1;
contents;
quit;

Check details of specific format (in the output window) in the Catalog:

Proc format fmtlib lib=sasformat.format1;
select $gender; run;

Output details of specific format in the Catalog to a dataset:

Proc format cntlout=gender lib=sasformat.format1;
select $gender; run;

Output multiple formats in the Catalog to a dataset:

Proc format cntlout=checkfmt lib=sasfmt.fmt;
select $sexfmt  $country $prorfmt $cuncfmt; 
run;

Setup Format Search Directory:

Options Fmtsearch = (sasformat.format1 work);

Turn Format Search Off:

Options Fmtsearch = ();

Hide Format Error Message and Format Error Note:

Options Fmtsearch = (sasformat.format1 work) NOFMTERR NONOTES;

Determine What Format Library is used for Format Search:

Proc Options option = FMTSEARCH;

Multiple Formats for Same Variable:

  • format name in the same library should unique;
  • format name is part of the definition of the varialbe that use the format.
  • format name is the only connection of the variable to the catalog.
  • by applying the same format name from different format libraries and catalogs in the fmtsearch option, you can have more than one format for each variable without changing the format name in the format statement in the data step. eg. english and french format for the same dataset.
Options Fmtsearch = (sasformat.englishfmt);
Options Fmtsearch = (sasformat.frenchfmt);

Delete Format Catalog:

Proc Dataasets Library=sasformat;
Delete englishfmt (memtype=catalog);
Delete frenchfmt (memtype = catalog);
Quit;

Apply Formats of Variables in a Dataset:

In a data step:

Format Var1  Formatname1 Var2 Formatname2;

Delete Formats of Variables in a Dataset:

In a data step:

Format Var1 Var2;
Format _all_;

Select and Exclude with Proc Format:

/* Select specific format, character format need to have $ prefix*/
/* adm is character format and ref is numeric format */
proc format library=formatlib cntlout=fmtname;
select $adm ref;
run;
/* Exclude specific format */
proc format library=formatlib cntlout=fmtname;
exclude $adm lastref;
run;

Note that Select and Exclude can not be used together in one Proc Format procedure.

SAS: PROC Format using dataset to create multiple formats

The following codes create two department formats: one formats ‘key’ variable to ‘deptid’ variable; the other formats ‘deptid’ variable to ‘deptname’ variable. Output the format in the work library. The convenience of the data step approach is to include all the related formats in one data set, eg. fm_dept. When the formats are required, simply using proc format and cntlin=dataset option to activate the formats.

data fm_depts;
set map_to_depts (keep = key deptid deptname);
length fmtname $ 8 type $ 1 label $70 start $ 10;
fmtname='$mapdepc';
start=key;
label=deptid;
type='c';
output;
fmtname='$mapdepn';
start=deptid;
label=deptname;
type='c';
output;
keep start fmtname label type;
run;
proc sort nodupkey; by fmtname start; run;
proc format library = work cntlin= fm_depts; run;

Format can also be created manually in the catalog as follows. A format catalog can contain many formats.

libname sasfmt  'X:\Work\SAS\data source\format';
proc format library =sasfmt.commonfmt;
run;
options fmtsearch = (sasfmt.commonfmt);
proc format library=sasfmt.commonfmt ;
value
$year
'2015' = '15'
'2016' = '16'
'2017' = '17'
'2018' = '18;
value
$ftpt
'FT' = '1'
'PT' = '2'
;
run;

SAS: Compare two datasets

Use PROC COMPARE  to compare attributes and variable existence of two dataset at multiple levels without using Data Step.

Results

Comparison of dataset1 with dataset2

(Method=EXACT)
Data Set Summary: Compare the number of variables, varibles with different labes, and number of observations
Variables Summary: number of same variables on both datasets; number of variables in one dateset but not the other.
Listing of Common Variables with Conflicting Types:
listing of Common Variables with Different Attributes: list variables with different length, formats, informats, and labels
Observation Summary
Values Comparison summary
Variables with Unequal Values
Value Comparison Results for Variables

options
NOVALUES to compare just the meta data
BRIEF to get a concise output to omit the variables with unequal values from the output.
LISTVAR to get a listing of the variables that are unique to one dataset or the other

meta data comparison:
proc compare base = dataset1 compare = dataset2 NOVALUES LISTVAR;
run;

record level comparison:

proc sort data= dataset1;
by studentid course;
proc sort data= dataset1;
by studentid course;
proc compare base = dataset1 compare = dataset2 out=check outnoequal;
by studentid courseid;
run;

OUTNOEQUAL: suppresses the writing of observations when all values are equal.  Only show the differences.