SAS on Unix
This introduction to SAS on UNIX provides basic information necessary to create and execute simple SAS programs. Execution of SAS programs on UNIX requires a UNIX (network) account on the computational server (i.e., FATMAN.SIU.EDU) and the ability to Telnet to and login this account. (Unix access can be obtained from the web.)
SAS Language
SAS on UNIX is virtually identical in syntax and statements to the mainframe or Windows versions of SAS. (For a summary of general SAS syntax, see the article Introduction to SAS.)
The following sections discuss the aspects of the SAS language that are distinctive to SAS on UNIX.
SAS Syntax
There are four important areas of SAS syntax: (1) variable names; (2) statement delimiters; (3) indentation; and (4) upper- and lowercase entry. For SAS on UNIX, the first three areas are identical to those from other SAS platforms. SAS on UNIX also allows the use of upper- and lowercase characters in data entry. This is especially useful when producing summary tables where capitalization of variable values is important (e.g., proper names).
DATA Step
The DATA step creates a SAS data set and provides: (1) a name for the data set (DATA statement); (2) access to the data lines (CARDS, INFILE, or SET statements); and/or (3) the names and formats of the variables (INPUT statement). As with the SAS syntax rules, a DATA step for SAS on UNIX is identical to other versions of SAS.
One statement that is used with interactive but not noninteractive versions of SAS is the RUN statement. The format of the statement is
RUN;
The RUN statement informs SAS that the DATA step is ready to be executed.
PROC Step
SAS on UNIX provides procedures for computing simple descriptive statistics (CORR, FREQ, MEANS, UNIVARIATE), for constructing summary tables (SUMMARY, TABULATE, FREQ), for plotting and charting data (PLOT, CHART), and for a large number of other functions. Due to our UNIX software license with SAS Institute, Inc., most SAS software associated with the UNIX platform is available.
As with the DATA step, a RUN statement must be included with each interactive procedure to inform SAS that the PROC is ready to be executed.
Noninteractive Program Execution
SAS programs may be executed in a noninteractive mode. Most SAS users find this is the simpliest and easiest way to execute a SAS program. A single program file containing all the needed SAS statements is created using a UNIX editor program (e.g., VI, Emacs, XC, or XE). This program file, named "fn.sas", is executed by issuing the command
SAS fn
where the "fn" is the name of the SAS program file. After execution of the program, two additional files are written to disk: "fn.log" and "fn.lst". The LOG file contains the SAS program log with error statements. Procedure output is found in the LST file.
For example, the file"sample.sas" contains the following SAS program:
DATA ONE; INPUT NAME $ TEST1 TEST2 TEST3; CARDS; SMITH 80 85 91 BROWN 92 89 94 ADAMS 79 86 82 PROC SORT; BY NAME; PROC PRINT;
To execute this file using the noninteractive mode, enter the command:
SAS SAMPLE
at the UNIX $ prompt. Two files are produced by the program execution: "sample.log" and "sample.lst". The "sample.log" file contains the SAS program log, with SAS warnings and error statements. The "sample.lst" file contains the output from the PRINT procedure (i.e., three observations with four variables each, sorted by NAME).
Interactive Program Execution
Interactive execution of SAS programs is accomplished through the use of the SAS Display Manager. To enter the Display Manager, type:SAS
at the UNIX $ prompt. Two of the three separate screens that comprise the SAS Display Manager will appear on the monitor: (1) the Program Editor screen, where the user enters SAS programming statements and data lines; and (2) the Log screen, containing the SAS program log with its messages, warnings, and error statements. The Output screen, which holds all procedure output produced by the SAS program appears after the program has been submitted for execution. Any two of the screens may be viewed simultaneously, or, using the ZOOM command, one screen may cover the entire monitor display area.
Program Function Keys
The interactive version of SAS on UNIX necessitates many moves from one screen to another (e.g., checking output, the program log, and error messages, and correcting and resubmitting programs). Function keys perform a designated task in a single keystroke and therefore increase the efficiency of the programmer. Following are Function keys provided by SAS on UNIX:
Key Function F1 help F2 F3 submit F4 pgm (Program Editor screen); recall F5 rchange F6 backward F7 forward F8 F9 F10 left
The cursor keys allow movement within a screen. For example, the F6 (BACKWARD) and F7 (FORWARD) keys scroll the screen (up and down) for viewing. To view a complete list of the function keys, type:
keys
from the command line within a window. (To exit the KEY window, type END on the command line.) The keys may be remapped by typing new commands in the KEYS window or by issuing the command:
KEYDEF keyname keydefinition
from the command line (e.g., from the Program Editor screen) where "keyname" is the specific key or key combination that is to be mapped and "keydefinition" is the command that is to be performed. The most common commands to be keyed are ZOOM, PGM, LOG, OUTPUT, BACKWARD, FORWARD, RECALL, SUBMIT, and END.
The DATA Step
Programming statements are input in the Program Editor screen. By placing the cursor in the Program Editor screen and pressing the ZOOM key, the entire viewing screen is made available for programming statements. The DATA step is typed, as well as any in-stream data lines, and a RUN statement. To submit the DATA step for execution, press F3 (SUBMIT). The viewing screen is once again a split screen, and the Display Manager will execute the DATA step.
To check for errors, move to the Log screen and examine the program log. If errors are present, return to the Program Editor screen (F4), recall the DATA step for editing (F4), correct the errors, and resubmit (F3) the DATA step. If no errors are present, return to the Program Editor screen (F4) and enter a second DATA step or SAS procedures.
The PROC Step
SAS procedure statements are entered in the Program Editor screen, each procedure followed by a RUN statement. Procedures may be submitted separately or as a PROC step. As with the DATA step, the Log screen is checked for errors, the procedure(s) recalled (F4) and corrected, and the Output screen viewed. If no errors are present, other procedures may be submitted or another DATA step created.
Ending A SAS Session
To end an interactive SAS session, enter the command:
BYE
from the Program Editor command line. The interactive session is terminated, and the user is returned to the UNIX $ prompt.
Additional Information
Differences between UNIX and other platforms of SAS are most often evident when accessing data files. The following sections provide sample programs for reading and writing data files (both raw data files and system files) and discussions of the SAS statements involved in the transactions.
- Raw Data Files -
Data for A SAS program may be found as in-stream data or as external files. The programs given below are written for the noninteractive mode of SAS on UNIX but, with the addition of RUN statements after each DATA step and procedure, may also be used with the interactive mode.
- Reading In-stream Data -
The most familiar method of incorporating raw data into A SAS program is with the CARDS statement. All data lines immediately follow this statement.
DATA ONE; INPUT NAME $ AGE GPA; CARDS; SMITH 31 3.89 BROWN 19 3.42 ADAMS 22 2.97 PROC SORT; BY NAME; PROC PRINT;
- Reading Data External to the Program -
When the data exist in a separate Unix file, the file is accessed via the INFILE statement.
DATA ONE; FILENAME SAMPLE 'EX4.DAT'; INFILE 'EX4.DAT'; DATA ONE; INPUT NAME $ AGE GPA; INFILE SAMPLE; INPUT NAME $ AGE GPA;
In the first example above, the INFILE statement informs SAS that the data are in the file "ex4.dat", which is found on the user's UNIX account. (If the external file is found in a location other than the user's login ID, the full pathname of the file is required.) In the second example, the INFILE statement references a name "sample", which is defined in the FILENAME statement as the data file "ex4.dat". The results of the DATA steps for the above examples are identical.
- Writing Data to an External File -
When data from a program need to be written to an external file, the SAS program contains a PUT statement to write the variable values, while a FILE statement directs the output to a specific external file.
DATA ONE; INPUT NAME $ TEST1 TEST2 TEST3; AVESCORE = MEAN(OF TEST1-TEST3); FILE 'RESULTS.TXT' NOTITLES; PUT NAME $ +1 AVESCORE; CARDS; SMITH 89 90 95 BROWN 76 82 86 ADAMS 80 89 85 PROC PRINT;
- System Files -
A SAS data set or system file, like raw data files, can be accessed from within a SAS program (i.e., in-stream) or from disk. The following sections provide sample program statements to access system files.
- Reading In-stream Data -
A SAS data set or system file from a program can be accessed from within the same program by the SET statement.
DATA ONE; INPUT NAME $ AGE GPA; CARDS; SMITH 31 3.89 BROWN 19 3.42 ADAMS 22 2.97 DATA TWO; SET ONE; IF AGE > 22;
In the above example, data set TWO is created and uses the SAS data set ONE as input. All observations that have the value of 23 or greater for AGE are entered into this new data set.
- Reading Data External to the Program -
System files that exist in external files may also be used as input into SAS on UNIX programs.
LIBNAME CLASS '/HOME1/JOHNDOE/'; DATA ONE; SET CLASS.SPRING; IF GPA > 3.0;
In the above example, the SET statement is again used to include the system file as program input. The two-part name "class.spring" informs SAS that the data set is an external file. The first part of the name refers SAS to the LIBNAME statement that specifies the UNIX account JOHNDOE where the file resides. The second part of the name is the "fn" of the system file.
- Writing Data External to the Program -System files that exist external to the SAS program may be written using a LIBNAME statement and a two-part dataset name on the DATA statement.
LIBNAME CLASS '/HOME1/JOHNDOE/'; DATA CLASS.SPRING; INPUT NAME $ AGE GPA; CARDS; SMITH 31 3.89 BROWN 19 3.42 ADAMS 22 2.97 PROC PRINT;
The resulting system file SPRING.SSD01 is written to the Unix account JOHNDOE.
Other Miscellaneous Information
All Unix commands and file names are case-sensitive. Although all examples in this document show uppercase names and SAS statements, this is strictly an editing convenience. In reality, lowercase file names and SAS statements were used. Actual character data values were entered in upper-, lower-, or mixed-case as appropriate to the situation.
The TELNET software from TCP3270 for Windows 95/NT Version 3.02 allows the best viewing of the SAS Display Manager. Older releases of TELNET software may display unusual characters for the boxes surrounding the Program Editor, LOG, and OUTPUT windows.
When using the SAS Display Manager, any program or output that is created during the interactive session exists only during that session. If you wish to save a program or the output displayed in the LOG or OUTPUT screens, you should issue a FILE command from the command line of the screen where the file is located:
FILE "filename.ext"
where "filename.ext" is the name of the file to be saved. In a similar manner, if you wish to use a program in the SAS Display Manager that exists on your Unix account, you should use an INCLUDE statement from the command line:
INCLUDE "filename.ext"
The file named "filename.ext" will be copied to the Program Editor screen for changes and/or program execution.
References
SAS Companion for UNIX Environments: Language. SAS Institute, Inc. 1993. (#56114; $25.95)SAS Companion for UNIX Environments: User Interfaces. SAS Institute, Inc. 1993. (#56113; $25.95)
The above manuals can be ordered from SAS Institute, Inc.:
SAS Institute, Inc.
Book Sales Department
SAS Campus Drive
Cary, North Carolina 27513-2414
Telephone: (919) 677-8000 (ext. 7001)

