/******************* URBAN INSTITUTE MACRO LIBRARY *********************
: Data_to_format
Macro: Create a format using values from a data set
Description
: Open code
Use
: Peter Tatian
Author
***********************************************************************/
Data_to_format(
%macro FmtLib=work, /* Optional: Format library (def. WORK) */
FmtName=, /* Format name (start w/$ for char format) */
DefaultLen=., /* Optional: Default format length */
MaxLen=., /* Optional: Maximum format length */
MinLen=., /* Optional: Minimum format length */
InDS=, /* Input data set */
Data=, /* Input data set (alias) */
Value=, /* Value to be formatted (num or char expression) */
Label=, /* Label to use for formatted value (char expression) */
NotSorted=N, /* Specify NOTSORTED option for format (Y/N) */
OtherLabel=, /* Optional: Label to use for values not in format (char string) */
Desc=, /* Optional: Description of format for catalog (char string) */
Contents=N, /* Optional: List contents of format catalog (Y/N) */
Print=Y /* Optional: Print resulting format (Y/N) */
);
/*************************** USAGE NOTES *****************************
:
SAMPLE CALLData_to_format(
%FmtLib=General,
FmtName=$anc12a,
Data=General.ANC2012,
Value=ANC2012,
Label=ANC2012_name,
OtherLabel="Not a valid ANC"
)$anc12a in General.Formats catalog that converts
create format in var ANC2012_name from
values given by var ANC2012 to labels
data set General.ANC2012.in ANC2012 will be labeled "Not a valid ANC"
Values not found
*********************************************************************/
/*************************** UPDATE NOTES ****************************
11/23/04 Added Data= parameter (alias for input data set name),
for required parameters.
check 03/30/06 Added Contents= and Desc= parameters.
10/22/06 Added NotSorted= parameter.
*********************************************************************/
***** ***** ***** MACRO SET UP ***** ***** *****;
%
%local type fmtcat;
Note_mput( macro=Data_to_format, msg=Starting macro. )
%
***** ***** ***** ERROR CHECKS ***** ***** *****;
%
%if %length( &InDS ) = 0 %then %let InDS = &Data;
%if %length( &InDS ) = 0 %then %do;
Err_mput( macro=Data_to_format,
%msg=Must provide an input data set in Data= or InDS= parameter. )
%goto err_exit;
%end;
%if %length( &FmtName ) = 0 %then %do;
Err_mput( macro=Data_to_format,
%msg=Must provide a format name in FmtName= parameter. )
%goto err_exit;
%end;
%if %length( &Value ) = 0 %then %do;
Err_mput( macro=Data_to_format,
%msg=Must provide a num. or char. expression in Value= parameter. )
%goto err_exit;
%end;
%if %length( &Label ) = 0 %then %do;
Err_mput( macro=Data_to_format,
%msg=Must provide a char. expression in Label= parameter. )
%goto err_exit;
%end;
***** ***** ***** MACRO BODY ***** ***** *****;
%
** TYPE = Type of format (Character/Numeric) **;
%
%if %substr( &fmtname, 1, 1 ) = $ %then
= C;
%let type else
%= N;
%let type
** FMTCAT = Full name of format catalog **;
%
%if %sysfunc( indexc( "&fmtlib", '.' ) ) > 0 %then
= &fmtlib;
%let fmtcat else
%= &fmtlib..formats;
%let fmtcat
_cntlin (compress=no);
data
$ 8;
length hlo
"&fmtname" type "&type" /*hlo " " */
retain fmtname &defaultlen max &maxlen min &minlen;
default
%if %mparam_is_yes( &NotSorted ) %then %do;
"s";
retain hlo
%end;%else %do;
" ";
retain hlo
%end;
%if &maxlen > 0 %then %do;
$ &maxlen;
length label
%end;
&InDS end=last;
set
= &label;
label = &value;
start
output;
%if %length( &otherlabel ) > 0 %then %do;
if last then do;
= trim( hlo ) || 'o';
hlo = &otherlabel;
label
output;
end;
%end;
keep fmtname type hlo label start default max min;
run;
=&fmtlib cntlin=_cntlin;
proc format library
run;
=work memtype=(data) nolist;
proc datasets library
delete _cntlin;
quit;
** Add format description to catalog **;
%
%if &desc ~= %then %do;
=&fmtcat;
proc catalog catalog%if &type = C %then %do;
substr( &fmtname, 2 ) (desc=&desc) /entrytype=formatc;
modify %
%end;%else %do;
&fmtname (desc=&desc) /entrytype=format;
modify
%end;
quit;
%end;
** Print entire format to output **;
%
%if %mparam_is_yes( &print ) %then %do;
=&fmtlib fmtlib;
proc format library&fmtname;
select
run;
%end;
** List contents of format catalog **;
%
%if %mparam_is_yes( &contents ) %then %do;
=&fmtcat;
proc catalog catalog
contents;
quit;
%end;
%goto exit;
:
%err_exitErr_mput( macro=Data_to_format, msg=Format &FmtName was not created. )
%
:
%exitNote_mput( macro=Data_to_format, msg=Exiting macro. )
%
***** ***** ***** CLEAN UP ***** ***** *****;
%
%mend Data_to_format;
/************************ UNCOMMENT TO TEST ***************************
"K:\Metro\PTatian\UISUG\Uiautos";
filename uiautos =(uiautos sasautos);
options sasautos
options mprint nosymbolgen nomlogic;
data A;$ 1 v_lbl $ 3-9;
input v
datalines;
A Label A
B Label B
C Label C
;
run;=A;
proc print dataData_to_format(
%FmtLib=work,
FmtName=$test,
Desc="Test format",
Data=A,
Value=v,
Label=v_lbl,
OtherLabel="Not a valid value",
DefaultLen=.,
MaxLen=.,
MinLen=.,
Print=Y,
Contents=Y
)=work;
proc datasets library
quit;/**********************************************************************/
Data to Format
Purpose: Create a format using values from a data set