I am inheriting a new project from a group who can provide me the SAS format catalogs that go along with the data sets, but they do not have the original SAS code that created the formats. I know that I can create a SAS data set with all of the attributes of the formats, but does anyone have a way of generating the actual SAS code from a format catalog? I know I can write this myself but I'm just hoping someone else has already done it /
To be clear, I have a catalog: library.custom_formats
I want to create the SAS code for the formats.
PROC FORMAT;
value fmtname
1=".....'
etc.
Thanks for your help!
OK, here is some simple code that worked. For my case, all of the formats (>500) were numeric and quite simple so I did not put any extra code in to deal with special situations. Seems to work fine.
/* Note that all of these are simple numeric formats, nothing fancy, no ranges */
/* The code I am writing is not flexible for other styles */
filename out "c:\temp\new_formats.sas"; /* text file to hold generated SAS code | */ |
data _null_;
set inout.cntlfmt(keep=fmtname start end label) end=eof;
by fmtname;
file out lrecl=100;
if first.fmtname then
do;
if _n_=1 then
do;
/*first record in file */
put @1 "/* The code was created on &sysdate at &systime */";
put @1 "PROC FORMAT;";
end; /* first record in file */
put @4 "Value" @10 fmtname;
end;
put @7 start " - " end @20 '="' Label +(-1) "" ;
if last.fmtname then put ";";
if eof then
put @1 "run;" ;
run;
/* Try to run the code */
%include "c:\temp\new_formats.sas";
title "Contents of the New Format Library";
/* Formats built from generated code */
proc format fmtlib library=work.formats;
run;
I don't, but curious as to why you want it?
Good question!
In my mind, hard coded proc format programs should be used as little as possible. In production like systems, it's much better to maintain look tables instead, which in turn can be used for creating formats.
CNTLOUT tables can be used in such manner, so I can't really see the benefit of generating proc format picture/value statements.
That would be my preference as well, if this were a production system, but it's not. It is a system that is still under development , but the researcher in charge of the project is moving from a different university to ours. The people who are developing it are turning over materials to us so we can continue to work on it. There are several components to this system and several different sets of formats. This one, large set of formats is the only one that did not have the original PROC FORMAT code, so I would like to be able to produce it to be consistent with the other parts of the project. It is not a criticial need, just something that would be nice to have.
I'll likely write the program myself to do this using PUT statements, if I decide it's something I have to have. I was just hoping someone here could point me to a code sample to save time.
I don't know how to use CNTLOUT but...if I were to accomplish the task that you need (and I am a "scientist and statistician", not programmer) I would do the following:
create a copy of your data, applying a FORMAT statement to the variables of interest
%let my_vars = ....;
data copy(rename = (&my_vars = &my_temp_vars));* you can do this in a PROC SQL select into...;
set have;
format &my_vars.;*this will remove the format attached;
run;
data combine(keep = final_value);
merge have copy;
by something_unique;
final_value = cats(&my_vars., "=", ' "&temp_vars." ');
run;
proc print data combine noobs;
run;
then copy/paste in a PROC FORMAT;value...
ehehe...
this is all early morning thinking,and you will need a %DO loop so you can %scan through your list of variables.
Anca.
Reeza Jan 29, 2014 6:03 PM (in response to Kathy Wisniewski)
I don't, but curious as to why you want it?
Well! I just got a newer computer and os going from Windows XP SP3 (32 bit?), to Win7 64 bit version. SAS says all my existing formats are now incompatible even though they are all stored on a server! I can sure see why you would want what the op is asking for.
Yes, that is one of the nice things about having the SAS code. It is easier to recreate the formats if you need to on another system. My original issue was that the format catalog was created on a 32 bit Wiindows system and I have 64 bit, so I ended up going to a different machine to create a transport file. Not awful, but PROC FORMAT code would have been easier.
Here is a tech support note that explains that approach:
If I had to do this, I would read the cntlout data set and generate proc format code by writing to a file with put statements. Do you just have plain vanilla formats/informats? Or do you have picture formats or something more complex? If you just have plain vanilla formats/informats, I don't think it would be too hard to write this program. If you made it a macro, you could quickly generate proc format code if this situation comes up again. Of course, you were hoping that somebody already wrote this macro so you would not have to. Sorry, but I don't have one, as I have never needed one.
OK, here is some simple code that worked. For my case, all of the formats (>500) were numeric and quite simple so I did not put any extra code in to deal with special situations. Seems to work fine.
/* Note that all of these are simple numeric formats, nothing fancy, no ranges */
/* The code I am writing is not flexible for other styles */
filename out "c:\temp\new_formats.sas"; /* text file to hold generated SAS code | */ |
data _null_;
set inout.cntlfmt(keep=fmtname start end label) end=eof;
by fmtname;
file out lrecl=100;
if first.fmtname then
do;
if _n_=1 then
do;
/*first record in file */
put @1 "/* The code was created on &sysdate at &systime */";
put @1 "PROC FORMAT;";
end; /* first record in file */
put @4 "Value" @10 fmtname;
end;
put @7 start " - " end @20 '="' Label +(-1) "" ;
if last.fmtname then put ";";
if eof then
put @1 "run;" ;
run;
/* Try to run the code */
%include "c:\temp\new_formats.sas";
title "Contents of the New Format Library";
/* Formats built from generated code */
proc format fmtlib library=work.formats;
run;
Nice!
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.