SAS Data Integration Studio, DataFlux Data Management Studio, SAS/ACCESS, SAS Data Loader for Hadoop and others

How to apply variables' values format permanently?

Reply
Frequent Contributor
Posts: 75

How to apply variables' values format permanently?

Like the title says, I have a format library for variables' values but now have to re-apply the formats on the data every time using SAS (or restarting, to be more exact).

I wonder if there's some way to apply the formats permanently on the data, so that we don't have to worry about that additional steps. For example, when sharing data, I don't really want to include both the data file and the format library in the package, but rather prefer to make it as compact as possible.

I also encounter this problem: After applying formats for the data set in WORK library, I save it as a permanent data set. At that time, the saving process works fine, the output data works fine, other things are fine.

So I close SAS and don't save the format library. When I reopen SAS, I fail to load the permanent data set, i.e., it comes to a blank table (although the file size is still ~100MB!). The log message is that "ERROR: Format ZFormat not found or couldn't be loaded for variable Z."

Respected Advisor
Posts: 4,935

Re: How to apply variables' values format permanently?

Posted in reply to NonSleeper

Store your formats in your SASUSER library with option LIB=SASUSER in the proc format statement and your formats will be stored in the FORMATS catalog of your SASUSER library. The SASUSER library is permanent which means that your format definitions will persist to future SAS sessions.

PG

PG
Frequent Contributor
Posts: 75

Re: How to apply variables' values format permanently?

Thanks for the advice. I did that and was able to create a format catalog within SASUSER. Some follow-up questions:

1. When I reload the permanent SAS file, it doesn't automatically "synchronize" with the format catalog in SASUSER. Does it mean I still need to apply formats on variables (e.g., like this: DATA HAVE; SET HAVE; FORMAT Var1 Var1Fmt.Smiley Wink ? Is there some statement for the permanent data file to recognize the format catalog and align its variables with the defined formats?

2. It seems like a separate format file is still needed when sharing data, isn't it?

Super User
Posts: 3,260

Re: How to apply variables' values format permanently?

Posted in reply to NonSleeper

If you want to share your permanent formats with other users then a better option is to store your formats in the SAS library called LIBRARY. If you use LIB = LIBRARY on your PROC FORMAT statement then your format will be stored the FORMATS catalog in LIBRARY. All users automatically have the LIBRARY library allocated and SAS searches this location for formats.

Super User
Super User
Posts: 7,997

Re: How to apply variables' values format permanently?

Posted in reply to NonSleeper

The others are answering your question with regards to format catalog.  My opinion on the matter however is to avoid format catalogs.  These are proprietary.  I would do one of two things.

- Have the code and full text in separate columns, i.e. code=variable, decode=put(variable,format), and have no formats applied to the variables. 

- Have the code with formats applied, and have a lookup table, e.g:

proc sql;

      create table FULL as

     select        A.CODE,

                      B.DECODE

     from          HAVE A

     left join      CODE_LIST B

     on             A.CODE=B.CODE;

quit;

Don't know what its like elsewhere but in my area XML is coming in a big way, i.e. text based, non-proprietary.

Trusted Advisor
Posts: 3,215

Re: How to apply variables' values format permanently?

Posted in reply to NonSleeper

Avoid storing formats in the SASUSER and the LIBRARY namings.

- SASUSER is personally and only open at the DMS (local desktop) in a single user approach.

  All modern EGUIDE SAS-Studio (EIP) are using some of app-server approaches (WS one of those) at the best setting it as read-only. Blocking any updaets/writes

- LIBRARY is the old name for all kind of executables (STEPLIB JOBLIB) being accessible. It still is functioning that way. Avoid conflict with that.

Where to store formats for your dataset?

- As long as they are belonging to that dataset you can store them in the same libname of the dataset.  That is why the library option with proc format is there

  Base SAS(R) 9.4 Procedures Guide, Third Edition

- Activating those formats in that library is needing to adjust the fmtsearch option

  SAS(R) 9.4 System Options: Reference, Third Edition    You see the library option still there in the search order as default after work.
  Setting the option wiht nofmterr  is allowing you to access the data without errors without formats.

In a more bigger organization you could get into the need for some release management on sas-formats having their own life-cycle. 

---->-- ja karman --<-----
Frequent Contributor
Posts: 75

Re: How to apply variables' values format permanently?

I export SAS file to SPSS, apply value labels on them, and import back. That seems to do the job but looks like a compromise and/or disloyalty.

Respected Advisor
Posts: 4,935

Re: How to apply variables' values format permanently?

Posted in reply to NonSleeper

What does the dataset look like when you import back from SPSS? Are the value labels in new character columns or are the numeric variables transformed into character?

PG
Frequent Contributor
Posts: 75

Re: How to apply variables' values format permanently?

The data imported from SPSS looks just fine, i.e., numeric values aren't transformed to characters. Yet it also creates a format library. So I'd take back the previous words, the problem I tried to tackle remains.

Super User
Posts: 11,343

Re: How to apply variables' values format permanently?

Posted in reply to NonSleeper

Anything involving formats tends to involve two pieces (IMHO) 1) where to store and 2) accessing.

The first is a format catalog in a permanent library. If multiple users need to use this transparently then that means a library everyone has at least read privileges to. (In a shared environment I would strongly recommend a limited number of users with write or delete privileges.)

Then every user has to have that library in their format search path. If using a server version I have to believe that can't be that difficult. If you aren't in a server environment or have a mixture then having an autoexec or similar program run at start with:

options Append(fmtsearch= (name(s) of libraries to search for formats));  (or INSERT)

which will add those libraries to the end of any currently assigned libraries to look for formats.

Warning: The format search path is an ordered list. If you have format. in multiple libraries then the first library encountered version will be used.

Super User
Posts: 5,518

Re: How to apply variables' values format permanently?

Posted in reply to NonSleeper

Another angle that you might want to consider:

options nofmterr;

This statement lets you work with the data set even if the formats are not available.  Any formats that can't be found will automatically be converted to simple formats such as 8. or $8.  So this won't get you the formatted values, but it will let you work with the data if the format library is unavailable.

Ask a Question
Discussion stats
  • 10 replies
  • 1727 views
  • 8 likes
  • 7 in conversation