BookmarkSubscribeRSS Feed
knargis160
Calcite | Level 5

I have spss dataset which I imported into sas to convert it to sas dataset. I want to have the formats saved in permanent datasets. Is there any datastep which can be added to save the formats in the final dataset?

5 REPLIES 5
PaigeMiller
Diamond | Level 26

Depends on what you mean by your question. First, are you talking about custom formats?

 

Variables may have formats applied to them, and so for example, in your data set, SAS knows that variable X1 has format CUSTF. applied to it (where CUSTF. is a custom format). However, the custom format definition is not stored in the SAS data set, it has to be provided in order for the data set to be opened. (The dataset can also be opened with the NOFMTERR option turned on).

 

A discussion of the possible methods to store data with custom formats is here: https://communities.sas.com/t5/SAS-Programming/How-to-share-SAS-data-set-with-custom-formats/m-p/598...

--
Paige Miller
RichardDeVen
Barite | Level 11

Can you show an example of the data and the format(s) in question ?

 

A FORMAT is a mapping (i.e. instructions for the transformation) of an underlying value to a representational value (string of characters).  FORMATs are utilized by data viewers (view table, data grid) and procedures for rendering output or grouping values. 


A data set variable that can have a format applied to it permanently according to information stored in the data set header (i.e. metadata), or temporarily during a PROC step using the FORMAT statement.  A FORMAT statement specified within a DATA step will add that 'association' to the variable in any data sets created by the step.  FORMAT is only one of several ATTRIBUTES that a variable may have, see the ATTRIB statement to learn more about the others which are INFORMAT, LENGTH, LABEL, TRANSCODE


The instructions for the system on how to apply the mapping (i.e.) is stored in a catalog entry. If the catalog holding the format is in the WORK library, the format is 'temporary' because the WORK folder is deleted when the SAS session ends.

 

In order to 'save' a format depends on your actual question, which is stated in a way hard to understand.

 

Suppose you have a permanent folder and libname active in your SAS session.

LIBNAME FMTLIB 'C:\MySAS\Permanent\Formats';

Every future SAS session will need the same LIBNAME statement in order to access the information in the 'permanent' folder in the same way as the current session.  You can add that statement to a personal autoexec.sas and not have to worry about it henceforth.

 

If you want to save the 'instructions' of one or more formats in an actual permanent data set use PROC FORMAT.  The output data set is called a 'formats' data set and has specific column names for re-processing the data set later into a format.

PROC FORMAT CNTLOUT=FMTLIB.SOME_SELECTED_FORMATS_DATA;
SELECT <format-name1-> ... <format-name-n>;

The formats data can be used to recreate a format. Use the CNTLIN= option.

PROC FORMAT CNTLIN=FMTLIB.SOME_SELECTED_FORMATS_DATA;
run;

If you want to save a formats catalog entry in a permanent folder use PROC CATALOG

* save format catalog entry in permanent library MYFMTLIB catalog FORMATS;
proc CATALOG catalog=WORK.FORMATS;
  copy OUT=MYFMTLIB.FORMATS;
  select XYZZY / ENTRYTYPE=FORMATC;
run;

A stock SAS installation will have custom formats stored in entries of a temporary catalog named WORK.FORMATS.  You can tell the system to use a different library, or a searchable list of libraries for the place to find format catalog entries.  Use the FMTSEARCH= system option.  The option can be set in an autoexec.sas or at any point in a program.

OPTIONS FMTSEARCH=(MYFMTLIB);

The system will search for a format in catalogs in this order

  • WORK.FORMATS
  • MYFMTLIB.FORMATS

WORK is always searched first, unless it is specifically named in the FMTSEARCH option setting.  After the following statement the search order is different and WORK is searched second.

OPTIONS FMTSEARCH=(MYFMTLIB WORK);

 

ChrisHemedinger
Community Manager

See this topic: How to import SPSS data files into SAS 

 

It includes instructions for saving SPSS labels as SAS formats.

Check out SAS Innovate on-demand content! Watch the main stage sessions, keynotes, and over 20 technical breakout sessions!
ChrisNZ
Tourmaline | Level 20

@ChrisHemedinger The article title looks like a question is being asked, which might be answered or not in the page. Since it is an article, it makes sense to reason that a solution is given. However, to remove any confusion, it might make sense to title a solution using a non-interrogative construct. Maybe simply by adding a colon: " How to: Import SPSS data files into SAS".

This just occurred to me, feel free to ignore my ravings. 🙂

Tom
Super User Tom
Super User

@knargis160 wrote:

I have spss dataset which I imported into sas to convert it to sas dataset. I want to have the formats saved in permanent datasets. Is there any datastep which can be added to save the formats in the final dataset?


The simple answer is NO.  In SPSS the value labels are stored with the data. In SAS you only store the NAME of the format. The actual definition of the format (x is display as y) is stored in a format catalog.  So make sure to keep the format catalog that the import step created if you want to be able to display the formatted value of the variables in future SAS sessions.  Make sure you know how to point the FMTSEARCH option to the catalog with the formats so you can use them.

 

If you want the decoded values actually stored in the same SAS dataset as the raw values (codes) then create new character variables to hold the decoded value.  You can use the PUT() function or the VVALUE() function.

So if you have a dataset name HAVE with a variable named X and a format named XFMT to decode the values of X then you can use something like this to make a dataset that also has a new variable named X_DECODED.

data want;
  set have;
  x_decoded = put(x,xfmt.);
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
  • 5 replies
  • 1482 views
  • 1 like
  • 6 in conversation