How to Use a Permanent SAS Data Set

advertisement
How to Use a Permanent SAS Data Set
(commands=useperm.sas)
Introduction:
In general, SAS data sets are not compatible across operating systems (Windows, Mac,
Unix, mainframe), but they are downwardly compatible across releases (e.g. SAS 6, SAS
8, SAS 9). A later release of SAS can generally read data sets from an earlier release, by
using an engine specification. SAS transport files can be used to move SAS data sets
across operating systems. This chapter discusses using permanent SAS data sets from
different releases of SAS.
There are two zipped files that contain SAS data sets for this workshop. More
information can be obtained about the variables in these data sets in Appendix III.
SASDATA1.ZIP contains the following version 6 SAS data sets:




Fitness.sd2
Gpa.sd2
March.sd2
Owen.sd2
SASDATA1.ZIP also contains a version 6 formats catalog called FORMATS.SC2. We
discuss how to use formats catalogs in Chapter 20.
SASDATA2.ZIP contains the following SAS version 8/9 data sets:





bank.sas7bdat
baseball.sas7bdat
business.sas7bdat
iris.sas7bdat
tecumseh.sas7bdat
SASDATA2.ZIP also contains a version 7 SAS data set with a short file extension
(ship.sd7). The name of this file will need to be changed to ship.sas7bdat before it can be
read into SAS
Step-By-Step Instructions for Using a Permanent SAS Data Set:
1. Determine the File type: The first step in using a SAS data set is to determine
what type of file it is (i.e. the operating system from which it originated and the
SAS release or engine used to create it). The table below shows the file extensions
used by SAS to distinguish between data sets created on different operating
systems and different releases of SAS.
1
NB: If you cannot see the file extensions on your computer, follow the steps below:



Go to Windows Explorer or My Computer
From the menus at the top select Tools…Folder Options…View
Make sure that “Hide file extensions for known file types” is NOT
selected.
Operating System
Windows
Windows
Windows
Unix
Macintosh
DOS
SAS Release
V7.0 to V9.0
V7.0 to V8.0
V6.08 to V6.12
V6.06 to V6.12
V6.10 to V6.12
V6.04 (PC SAS)
Extension
.sas7bdat*
.sd7**
.sd2
.ssd01
.ssd01
.ssd
Example
business.sas7bdat
ship.sd7
fitness.sd2
mydata.ssd01
mydata.ssd01
mydata.ssd
*This long file extension is the default SAS 8.0 and 9.0 data set extension.
**Files ending in .sd7 will be written by SAS release 7 or 8 if there are already SAS 6
files in the same folder (such as .sd2 files). You may not be able to read .sd7 files if there
are also .sas7bdat files in the same directory. (SAS release 9 does not read data files with
the .sd7 extension at all.) If you do have SAS data sets that end in the .sd7 file
extension, rename them to .sas7bdat before trying to use them in SAS.
2. Assign a Library and Engine: The next step is to assign a library and engine so
that SAS can access the data set(s).
Library:
A library is a location on your computer (e.g. a folder or directory) where you store SAS
data sets. The libname statement assigns an alias (called a libref) to the directory that you
specify. The libref must be 8 characters or less to be valid in SAS.
A library refers to the entire folder (not to an individual data set). One library can have
several data sets stored in it, and they can be of mixed types. However, a particular
engine assigned to a given folder will only "SEE" files of one type. It is good practice to
keep SAS data sets of different types in different folders.
Default library:
If you do not specify a library for a data set, the default is the WORK library. If you have
no data sets in WORK, and no library is specified for a procedure, SAS will produce an
error stating that there is not a default data set to use.
Engine:
An engine tells SAS the type of files that it is to read. Some commonly used engines are:
2




V9 the default engine to read/write data sets for SAS release 9.
V8 the default engine to read/write data sets for SAS release 8.
V6 to read/write data sets in the SAS 6.08 through 6.12 format using SAS 8 or 9.
V604 to read (but not write) data sets created using PC SAS 6.04.
Note: Versions of SAS are downwardly compatible. That is: in SAS 6, you can use the
V6 and V604 engines, but not the V8 engine. In SAS 8 and 9, you can use all of these
engines to read files, but you cannot write a data set using the V604 engine.
Default engine:
If you do not assign an engine, SAS will assign one based on the SAS data sets found
within a folder. If there are no data sets in a folder, SAS will use the current version of
SAS as the default; i.e., if you are running SAS 9.0, it will automatically use the V9
engine, etc.
If there are already data sets within a folder, SAS will assign the engine corresponding to
the highest version compatible with any of the data sets in the folder. So if there are
both V6 and V9 data sets in a folder, SAS will assign the V9 engine to read data from
that folder. To read V6 data sets from a folder containing both V6 and V9 data sets, the
V6 engine must be used explicitly.
Instructions for Windows:
Suppose you have a number of SAS release 6 data sets stored in the c:\temp\sasdata1
folder (e.g. FITNESS.SD2, GPA.SD2 and MARCH.SD2). You need to submit a libname
statement from the program editor window for SAS to be able to utilize these data sets.
The libref assigned in the statement below is sasdata1. The engine that is used to read
the data sets is V6, the folder containing the data sets is "c:\temp\sasdata1", but it could
point to any folder that contains version 6 SAS data sets.
libname sasdata1 V6 "c:\temp\sasdata1";
SAS will not produce any output in the output window as a result of submitting these
commands, but you will see the note shown below in the SAS Log. Be sure to check the
SAS log after submitting a libname statement.
libname sasdata1 V6 "c:\temp\sasdata1";
NOTE: Libref SASDATA1 was successfully assigned as follows:
Engine:
V6
Physical Name: c:\temp\sasdata1
Once the libname statement has been submitted (no run statement is needed), you will be
able to use any of the SAS release 6 data sets in the c:\temp\sasdata1 folder. You will
need to specify the data set to use with the data= option for each procedure. The libname
3
statement will be in effect for the entire SAS session, and so it only needs to be submitted
once.
To use a particular data set, you must use the data= option, and specify a two-level name
for the data set (e.g., sasdata1.fitness). No spaces are allowed in the two-level data set
name. The data set that you specify with the data= option is only in effect for a given
proc and must be repeated for each proc, as shown in the example below. The file
extension is not specified. SAS assumes that the file extension will conform to the ruls
for file extensions that correspond to the engine you specified.
libname sasdata1 V6 "c:\temp\sasdata1";
title "SASDATA1.FITNESS";
proc means data=sasdata1.fitness;
proc print data=sasdata1.fitness;
run;
proc freq data=sasdata1.fitness;
tables sex;
run;
title "SASDATA1.GPA";
proc means data=sasdata1.gpa;
run;
You can have an unlimited number of libname statements in a single SAS session, to
allow you to utilize SAS data sets of different types and/or in different locations.
libname sasdata1 v6
libname sasdata2 v9
"c:\temp\sasdata1";
"c:\temp\sasdata2";
title "Contents of Version 6 Datasets in SASDATA1 Library";
proc contents data=sasdata1._all_ nods;
run;
Contents of Version 6 Datasets in SASDATA1 Library
The CONTENTS Procedure
Directory
Libref
SASDATA1
Engine
V6
Physical Name c:\temp\sasdata1
File Name
c:\temp\sasdata1
#
Name
Member
Type
File
Size
1
2
3
4
FITNESS
FORMATS
GPA
MARCH
DATA
CATALOG
DATA
DATA
8448
12544
16640
57600
Last Modified
31Dec96:08:24:38
20Jun02:07:23:54
31Dec96:08:24:38
21Dec93:08:28:46
title "Contents of Datasets in SASDATA2 Library";
proc contents data=sasdata2._all_ nods;run;
Contents of Datasets in SASDATA2 Library
The CONTENTS Procedure
4
Directory
Libref
SASDATA2
Engine
V9
Physical Name c:\temp\sasdata2
File Name
c:\temp\sasdata2
#
1
2
3
4
Member
Name
BASEBALL
BUSINESS
IRIS
TECUMSEH
File
Type
Size
DATA
DATA
DATA
DATA
82944
17408
13312
1147904
Last Modified
20Jun02:07:12:32
20Jun02:07:12:32
20Jun02:07:12:32
02Jun05:00:00:04
Assigning the Library by Using the New Library Icon:
To make sure a library will still be assigned in a later session, you can set it up using the
New Library icon in the menus, and select “Enable at startup”.
Assigning a Default Data Set:
A default data set can be assigned with an options _last_= statement after a libname
statement. (Be sure you have no blanks in _last_.) This allows you to utilize the same
data set without having to specify it for each procedure. In the example below,
sasdata1.fitness will be used for all procedures.
libname sasdata2 V9 "c:\temp\sasdata2";
options _last_=sasdata2.baseball;
title “SASDATA2.BASEBALL Data Set”;
proc means;
run;
proc freq;
tables team;
run;
proc reg;
model salary = cr_home;
run; quit;
The default data set will be in effect until a new one is specified with another options
statement, or until another new data set is created.
Note on Using Permanent Data Sets in SAS Release 8 and 9:
You can specify permanent SAS data sets to use by giving the complete path and file
name in quotes, starting with SAS release 8. This avoids the libname statement, but does
not allow a default data set to be specified. Data set options (e.g., obs= ) can still be
specified in parentheses after the quoted file name.
title “SASDATA2.IRIS Data Set”;
proc means data="c:\temp\sasdata2\iris.sas7bdat";
proc print data="c:\temp\sasdata2\iris.sas7bdat"(obs=10);
run;
5
How to Open a Permanent SAS Data set in SAS/INSIGHT:
Activate SAS/INSIGHT. In the dialog box that appears choose the library that you wish
to use (e.g. SASDATA2), then double-click on the data set that you wish to open (e.g.
BUSINESS). To open another data set, simply go to File…Open…and choose the data
set to open.
How to create a temporary SAS data set from a permanent one:
Many SAS users simply create a temporary SAS data set to use in a given session. This
temporary data set becomes the default automatically.
libname sasdata2 V9 "c:\temp\sasdata2";
data business;
set sasdata2.business; run;
title “Business data set”;
proc means;
run;
This method has the advantage of allowing you to work with a temporary SAS data set,
which is often simpler than working with a permanent one. But it can be cumbersome if
you have a large data set, because it creates a whole new copy of the data in the WORK
library.
How to de-assign a library:
Use the libname statement with the option clear to de-assign a library. The library
assignment will be cleared, but the data sets in the library will not be affected. Do not
specify an engine here.
libname sasdata1 clear;
Autoexec.sas file:
The library or libraries that you wish to use must be re-assigned for each session, if you
assign them using a libname statement, To have SAS remember your libraries from one
run to another, you can create a file called autoexec.sas, and place the libname
statements in it, as shown below. Each time SAS starts up, it will read the autoexec.sas
file, and assign the appropriate libraries.
libname sasdata1 V6 "c:\temp\sasdata1";
libname sasdata2 v9 "c:\temp\sasdata2";
If you place the autoexec.sas file in the directory from which SAS is running, SAS will
read it and execute the commands it contains each time it starts up. However, if you save
the autoexec.sas file in another location, you can specify it as an option in the SAS
6
shortcut. An example SAS shortcut is shown below, followed by the notes in the SAS
Log. The shortcut shown below would all appear on a single line.
"C:\Program Files\SAS\SAS 9.1\sas.exe" -CONFIG "C:\Program Files\SAS\SAS
9.1\nls\en\SASV9.CFG" –AUTOEXEC “c:\temp\autoexec.sas”
NOTE: AUTOEXEC processing beginning; file is c:\temp\autoexec.sas.
NOTE: Libref SASDATA1 was successfully assigned as follows:
Engine:
V6
Physical Name: c:\temp\sasdata1
NOTE: Libref SASDATA2 was successfully assigned as follows:
Engine:
V9
Physical Name: c:\temp\sasdata2
NOTE: AUTOEXEC processing completed.
7
Download