Not sure this is what you are looking fo
/* T005670 Creating format source code from a format catalog -- John Groenfeld
inspired by
https://goo.gl/a25zs7
https://communities.sas.com/t5/SAS-Procedures/read-catalog/m-p/341691
HAVE A COMPILED FORMAT
======================
Contents of Catalog WORK.FORMATS
# Name Type
--------------------------
1 CITY FORMATC
WANT
====
value $city
'BR1' = 'Birmingham UK'
'BR2' = 'Plymouth UK'
'BR3' = 'York UK'
'US1' = 'Denver USA'
'US2' = 'Miami USA'
other = 'INCORRECT CODE'
;
run;quit;
WORKING CODE
%utl_getfmt(outfile=d:/txt/city.txt,myfile=myformats,mytype=C,myfmt=CITY);
FULL SOLUTION
=============
proc datasets lib=work kill;
run;quit;
%symdel outfile myfmt myfile mytype / nowarn; * just in case;
* create and compile format $city;
proc format lib=work;
value $city
'BR1' = 'Birmingham UK'
'BR2' = 'Plymouth UK'
'BR3' = 'York UK'
'US1' = 'Denver USA'
'US2' = 'Miami USA'
other = 'INCORRECT CODE'
;
run;quit;
proc format library=work cntlout=myformats;
run;quit;
%let pgm=utl_mkefmtsas;
/* this macro generates SAS program to re-create a user-defined format */
/* utiltity to delete sas code if you are rerunning */
%macro utl_fkil ( utlfkil ) / des="delete an external file";
%local urc;
%let urc = %sysfunc(filename(fname,%quote(&utlfkil)));
%if &urc = 0 and %sysfunc(fexist(&fname)) %then
%let urc = %sysfunc(fdelete(&fname));
%let urc = %sysfunc(filename(fname,''));
run;
%mend utl_fkil
;
/* create the format code */
%macro utl_getfmt(outfile=,myfmt=,myfile=,mytype=) ;
/* This work is based on John Groenfeld macro in SAS-L */
/* Program will fail if format type is other than P C I N or J */
/* I have tried to cover all kinds of format but I am no sure */
%put %sysfunc(ifc(%sysevalf(%superq(outfile)=,boolean),**** Please Provide output fileref for sas format code ****,));
%put %sysfunc(ifc(%sysevalf(%superq(myfile )=,boolean),**** Please Provide an input format control dataset ****,));
%put %sysfunc(ifc(%sysevalf(%superq(mytype )=,boolean),**** Please Provide antype of format P C I N J only ****,));
%put %sysfunc(ifc(%sysevalf(%superq(myfmt )=,boolean),**** Please Provide format name ****,));
%let res= %eval
(
%sysfunc(ifc(%sysevalf(%superq(outfile)=,boolean),1,0))
+ %sysfunc(ifc(%sysevalf(%superq(myfile )=,boolean),1,0))
+ %sysfunc(ifc(%sysevalf(%superq(mytype )=,boolean),1,0))
+ %sysfunc(ifc(%sysevalf(%superq(myfmt )=,boolean),1,0))
);
%if &res = 0 %then %do;
/*
/* tetcases without macro
%let outfile =outf;
%let myfile =fmt001;
%let mytype =P;
%let myfmt =SALARY;
*/
data _null_ ;
file "&outfile" mod ;
set &myfile (where=(fmtname="&myfmt" and type="&mytype")) end=dne;
/* global stuff */
if index(upcase(start),'*OTHER*' ) then start = compress(start,'*') ;
if index(upcase(end), '*OTHER*' ) then end = compress(start,'*') ;
select (type);
when ('P') do;
if _n_=1 then put "picture " "&myfmt" ;
link other;
link quotelabel;
link noquotestart;
if upcase(start) ne 'OTHER' then do;
put '(mult=' mult best12. ;
if not missing(prefix) then do ;
prefix=cats('"',prefix,'"');
put 'prefix=' prefix ;
end ;
if not missing(fill) then do ;
fill=cats('"',fill,'"');
put 'fill=' fill;
end ;
put ')' ;
end;
end;
when ('C') do;
if _n_=1 then put "value " "$&myfmt";
link other;
link quotelabel;
link quotestart;
end;
when ('I') do;
if _n_=1 then put "invalue " "&myfmt" ;
link other;
link noquotelabel;
link quotestart;
end;
when ('N') do;
if _n_=1 then put "value " "&myfmt" ;
link other;
link quotelabel;
link noquotestart;
end;
when ('J') do;
if _n_=1 then put "invalue " "$&myfmt";
link other;
link quotelabel;
link quotestart;
end;
end;
if dne then put ';';
return;
other:
if index(upcase(start),'*OTHER*' ) then start = compress(start,'*') ;
if index(upcase(end), '*OTHER*' ) then end = compress(start,'*') ;
return;
noquotestart:
if start ne end then put start ' - ' end '= ' label;
else put start '= ' label;
return;
quotestart:
if upcase(start) ne 'OTHER' then start = cats('"',start,'"');
if upcase(end) ne 'OTHER' then end = cats('"',end, '"');
if start ne end then put start ' - ' end '= ' label;
else put start '= ' label;
return;
quotelabel:
if hlo=:'LF' then label = cats('[',label,']') ;
else label = cats('"',label, '"');
return;
/* documentation purposes only */
noquotelabel:
label=label;
return;
run ;
%end;
%mend utl_getfmt;
proc catalog cat=work.formats;
contents;
run;quit;
... View more