BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
KathyWiz
Calcite | Level 5

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 Smiley Happy/

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!

1 ACCEPTED SOLUTION

Accepted Solutions
KathyWiz
Calcite | Level 5

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;

View solution in original post

10 REPLIES 10
Reeza
Super User

I don't, but curious as to why you want it?

KathyWiz
Calcite | Level 5

Good question!

  • The first reason is that we want to be able to modify the formats.  It is easier to do that from SAS code than from editing the SAS datasets that get input into PROC FORMAT.  There are hundreds of formats and navigating text is easier than editing a data set.
  • The other reason is that I work with a lot of novice SAS programmers. They are great scientists and statisticians, but aren't that familiar with things like SAS catalogs. They are much happier if I can give them a SAS program with the PROC FORMAT code that they can modify as they like.  They are concentrating on the statistics and the science and having an easily editable text file with SAS code makes them happy.  I like to help them out where I can.
LinusH
Tourmaline | Level 20

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.

Data never sleeps
KathyWiz
Calcite | Level 5

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.

AncaTilea
Pyrite | Level 9

I don't know how to use CNTLOUT Smiley Sad 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.

rickpaulos
Obsidian | Level 7

 

      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.

KathyWiz
Calcite | Level 5

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:

22194 - How to use the CNTLOUT= and CNTLIN= options in PROC FORMAT to move formats from one platform...

Brit
Calcite | Level 5

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.  

KathyWiz
Calcite | Level 5

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;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 10 replies
  • 1438 views
  • 1 like
  • 6 in conversation