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

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."

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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.

 

Editor's note: this is a popular topic so we're adding some more details to address different scenarios:

 

  • First, as the OP is coming from SPSS (where data labels are part of the SPSS data file), SAS formats may be a new concept.  We explain this a bit in this topic about importing SPSS files.
  • If the goal is to avoid errors when the formats aren't available, the options nofmterr suggestion from @Astounding is good advice. 
  • If you want to disentangle the data set from the need for formats, you can create a separate table with the lookup values and join when you need it for reporting.  The advice from @RW9 accomplishes that.

View solution in original post

11 REPLIES 11
PGStats
Opal | Level 21

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
NonSleeper
Quartz | Level 8

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.;) ? 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?

SASKiwi
PROC Star

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.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

jakarman
Barite | Level 11

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 --<-----
NonSleeper
Quartz | Level 8

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.

PGStats
Opal | Level 21

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
NonSleeper
Quartz | Level 8

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.

ballardw
Super User

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.

 

Editor's note: this is a popular topic so we're adding some more details to address different scenarios:

 

  • First, as the OP is coming from SPSS (where data labels are part of the SPSS data file), SAS formats may be a new concept.  We explain this a bit in this topic about importing SPSS files.
  • If the goal is to avoid errors when the formats aren't available, the options nofmterr suggestion from @Astounding is good advice. 
  • If you want to disentangle the data set from the need for formats, you can create a separate table with the lookup values and join when you need it for reporting.  The advice from @RW9 accomplishes that.
Astounding
PROC Star

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.

hmoghul
Fluorite | Level 6

 

step1. You may save all the formats in a single sas program at your permanent library. 

step2. Next you can retrieve formats using %include macro as below: 

 

%let path=file-path; 

libname xyz "&path";

%include 'file-path\allformats.sas';

run;

 

proc print data=xyz.abc;

format gender $gender. salary $sal.

run;

 

 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 11 replies
  • 9463 views
  • 9 likes
  • 8 in conversation