BookmarkSubscribeRSS Feed
jnivi
Calcite | Level 5

Hi there, 

 

I have a question about permanently saving value formats in SAS. For example,  I have included my sample code for a variable in my "sample" dataset.  

 

proc format; 

      value sex 

      1="Male"

      2="Female"

 

data sampledata;
     set sampledata;
     format sex sex.;
      run; 

 

After running the above code, I am able to see the actual formats in my dataset (i.e male /female show up as the actual values in the dataset).  Once I save the dataset, and reload this dataset in a new session, I receive an error message stating that the format for sex can be found. 

 

I am attempting to apply this value format, to several variables in my dataset. I will also be sharing this dataset for others to use as well, so I was hoping there was a way to save the formats permanently so that I and others can see the actual value labels when the dataset is loaded in SAS.  I also tried opening my dataset in R, and I can see the formats were not automatically loaded ( i.e sex values are 1 and 2, instead of male and female). 

 

The reason why I wanted the labels, but still wanted the variable to remain numeric is because I am working with multiple large datasets (some datasets being as large as 80GB) when the variables are formatted as character, so saving the variables as numeric with labels significantly reduces the size of the dataset. 

 

Thank you so much for taking the time to read through my message, and I greatly appreciate any tips possible to solve my issue!

4 REPLIES 4
Tom
Super User Tom
Super User

Formats are independent of the variables you use them with.  So they are stored separately.  Formats are stored in a SAS catalog. Your PROC FORMAT statement does not say where to write the new formats, so the default catalog of WORK.FORMATS is used.

 

To save a dataset permanently you put it into a "library" by using a two level name. The same thing applies to format catalogs.

 

For SAS to find the stored formats the catalog where they live needs to be included in the FMTSEARCH system option settings.  Since the default setting includes the WORK.FORMATS catalog then formats you created there are found.  But when you start a new session the WORK library is empty.

 

libname mylib 'some path'; 
proc format lib=mylib.formats;
  value sex 
    1="Male"
    2="Female"
  ;
run;
options append=(fmtsearch=(mylib.formats));
data mylib.sampledata;
   set sampledata;
   format sex sex.;
run; 

Then when you want to use them in a new session just define the libref and make sure that the catalog is included in the search path.

libname mylib 'some path'; 
options append=(fmtsearch=(mylib.formats));
proc freq data=mylib.sampledata;
  tables sex;
run;

 

 

 

Ksharp
Super User

Tom already gave you the solution.

Alternative way is copying it into your WORK library.

 


libname x 'c:\temp\'; 
proc catalog ;
copy in=x.formats out=work.formats;
quit;

data x;
 input x;
format x x.;
cards;
1
2
;
run;
FreelanceReinh
Jade | Level 19

Hi @jnivi,

 

The default setting of system option FMTSEARCH includes the libref LIBRARY, as shown in the PROC OPTIONS log below:

31    proc options option=fmtsearch;
32    run;

    SAS (r) Proprietary Software Release 9.4  TS1M5

 FMTSEARCH=(WORK LIBRARY)
                   Specifies the order in which format catalogs are searched.
NOTE: PROCEDURE OPTIONS used (Total process time):

This means that user-defined formats and informats in a format catalog formats.sas7bcat stored in some directory (cf. the PROC FORMAT step in Tom's post) will be found if the libref LIBRARY is assigned to that directory (and if there is no format/informat with the same name in WORK.FORMATS). So in this situation you can just define

libname library 'some path';
Ksharp
Super User

As Freelance said, another way is here:

libname x 'c:\temp\'; 
options fmtsearch=(x);
data x;
 input x;
format x x.;
cards;
1
2
;
run;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 4 replies
  • 363 views
  • 4 likes
  • 4 in conversation