Saving SAS Datasets
This document will explain how to save SAS datasets and use those permanent datasets at a later date.
Saving a dataset in SAS is not as simple as selecting "save" from the File menu. In fact, if you do select Save you can save either your SAS log, your SAS program or your SAS output, but not your SAS dataset. When you import a file into SAS, whether from Excel, Access or any other application, by default it is saved in working memory. Working memory is just that, memory that is available when you are working in SAS. Once you end your SAS session, that dataset is gone. Think of it as if you were typing a file in a word processor and then you quit without saving your document. You might think you select Save from a menu to save your SAS dataset as a permanent file. That is very logical, but it doesn’t work. If you are in the Editor window and from the File menu you select Save, you will save your SAS program. That is a good thing to do but it doesn’t save your SAS dataset. If you are in the Log window and from the File menu you select Save, you will save your SAS log. That may be a good thing to do but it doesn’t save your SAS dataset. If you are in the Output window and from the File menu you select Save, you will save your SAS output. That may be a good thing to do but it doesn’t save your SAS dataset. Usually, you will want your output in some other form, such as an HTML, PDF or RTF file. This can be done using the Output Delivery System. So how do you save your SAS dataset? To save your SAS dataset as a permanent file, you use a Libname statement as shown in the example below. Libname perm “c:\temp” ;Data perm.internet ;
set tempdata ; The statements above will save the dataset tempdata under the name Internet.sas7bdat in the temp folder on the c drive. The suffix sas7bdat is added automatically. You only need to specify the Libname statement once and you can use the same libref throughout your program. The name “perm” used above can be any valid SAS name. This name is referred to in SAS documentation as a libref or ‘library reference’. Anywhere it is used in your program it will refer to the folder specified in the Libname statement, in this case, the temp folder. The general format for saving your dataset as a permanent SAS file is shown below. Libname libref “directory for data” ;
Data libref.datasetname ;
set datasetname2 ;
NOTE: The folder named in the LIBNAME statement must already exist. Also, you must have authorization to write to that folder. Many of the folders on the lab computers do NOT give write access to users Note 2: If you have a flash drive (usually that will be the E drive, you can save to that, by using the statement Libname libref “e:\” ; or you can copy your files on to the flash drive later. Just copy them from the temp directory or wherever you saved them. If you want to use a permanent SAS dataset in another program at a later date, you would use the Libname statement to specify the directory, as in the following short program: Libname in “c:\temp” ;
Proc print data =in.internet ;
Run ;
Last updated:
February 24, 2010