Writing and Executing A SAS Program
Writing a SAS Program
Now that we have looked at various steps that go into creating a SAS program, the next step is to write one. The example used here is for the data set discussed earlier. A SAS statement may begin in any column on a line. However, for the sake of clarity, lines starting with DATA or PROC statements begin in column 1; all other statements are indented. In the following example SAS is asked to create a data set named anxiety after reading the data steps. The data are embedded within the SAS program.
A SAS program in its simple form can be as follows:
DATA anxiety; INPUT id 1-2 sex $ 3 exp 4 school 5 c1 6 c2 7 c3 8 c4 9 c5 10 c6 11 c7 12 c8 13 c9 14 c10 15 m1 16 m2 17 m3 18 m4 19 m5 20 m6 21 m7 22 m8 23 m9 24 m10 25 mathscor 26-27 compscor 28-29; CARDS; [data lines entered here] ; PROC PRINT; VAR sex school exp mathscor compscor; PROC FREQ DATA=anxiety; TABLES sex exp school; ENDSAS;
The input format specified in the above example can also be written in a shorter form with a mixed style column and formatted input.
In this case the program will read variable id from columns 1-2 and sex from column 3. The next two variables exp and school have a width of 1 column each and start in column 4. The variables c1 through c10 (10 variables in sequential order) have a width of 1 column each and start immediately after the variable school.
If you wanted to read only the variables id and the last 2 variables (mathscor, compscor) you could write the INPUT statement as:
The @ moves the pointer to column 26 and reads two variables with a width of 2 columns each.
The example below illustrates the above data set being read by SAS from an external file named clas.dat stored in the same directory. Comments are provided for documentation purposes. Statements enclosed in /* .... */ or *......; are ignored by SAS and will not be used while executing the program. The TITLE statement is used to provide title lines to be printed on the output file.
* Computer Anxiety in Middle School Children; /* The following procedure specifies value labels for variables */ PROC FORMAT; VALUE $sex 'M'='male' 'F'='female'; VALUE exp 1='upto 1 year' 2='2-3 yrs' 3='3+ yrs'; VALUE school 1='rural' 2='city' 3='suburban'; DATA anxiety; INFILE '/pathname/clas.dat'; /* remember that Unix is case sensitive, so anything within single quotes must have the proper case. For example, clas.dat and CLAS.DAT are two different files */ INPUT ID 1-2 SEX $ 3 (exp school) (1.) (c1-c10) (1.) (m1-m10) (1.) (mathscor compscor) (2.); FORMAT sex $sex. exp exp. school school.; /* conditional transformation. Missing values are declared .*/ IF mathscor=99 THEN mathscor=.; IF compscor=99 THEN compscor=.; /* recoding variables. Several items are to be reversed in scoring. */ /* the Likert type questionnaire had a choice range of 1-5 */ C3=6-c3; c5=6-c5; c6=6-c6; c10=6-c10; m3=6-m3; m7=6-m7; m8=6-m8; m9=6-m9; compopi = sum (of c1-c10) /*Find sum of 10 items using SUM function */; mathatti = m1+m2+m3+m4+m5+m6+m7+m8+m9+m10 /* Add item by item */; LABEL id='STUDENT IDENTIFICATION' sex='STUDENT GENDER' exp='YRS OF COMP EXPERIENCE' school='SCHOOL REPRESENTING' mathscor='SCORE IN MATHEMATICS' compscor='SCORE IN COMPUTER SCIENCE' compopi='TOTAL FOR COMP SURVEY' mathatti='TOTAL FOR MATH ATTI SCALE'; PROC PRINT; VAR exp school mathscor compscor compopi mathatti; TITLE 'LISTING OF THE VARIABLES'; PROC FREQ DATA=anxiety; TABLES sex exp school; TABLES (exp school)*sex; TITLE 'FREQUENCY COUNT'; PROC MEANS DATA=anxiety; VAR compopi mathatti mathscor compscor; TITLE 'Descriptive Statistics'; * The following procedure creates a data subset with males alone; DATA males; SET anxiety; IF sex EQ 'M'; * sorting the data by school to use the CLASS statement in printing; * out the means for each school system in a following step; PROC SORT DATA=males; BY school; PROC MEANS DATA=males; CLASS school; VAR compopi mathatti mathscor compscor; ENDSAS;
Modes of Execution
Suppose that you saved the above program in to a file, clas.sas, in your root directory. Selecting a mode of execution to run your SAS job depends on the type of job you run, and the system you work with. Because Unix operating systems are multitasking systems, you can run various processes at the same time. For example, you can have one process running in the foreground and several in the background. A foreground process executes while you wait. The terminal cannot be used while the job is running. A background process executes independently of the shell, and the terminal is free for other computing activities. SAS interactive line mode, display manager mode, and non-interactive mode all run in the foreground. A batch mode runs in the background. Because Unix is case sensitive, use lowercase when you invoke SAS. Within SAS you may use upper or lower case.
A SAS job creates temporary scratch files during every mode of execution. These scratch files will be deleted once the execution is complete. In some instances your disk may not have enough space to accommodate the scratch file. In such a situation you can redirect your scratch file to a temporary system disk (/tmp and /scr). This is done while invoking SAS:
The -work parameter reroutes the scratch file to a temporary system disk (/tmp). You may replace /tmp with /scr. You may also copy your large files into these directories for temporary use.
If you do not have a full-screen terminal (e.g., vt100) and want to run SAS in interactive mode, you may use interactive line mode. If you do have a full-screen terminal, you have the option of running your SAS programs in either interactive line mode or display manager mode. To start an interactive line mode, at the system prompt, type:
The option "nodms" stands for No Display Manager.
Now the system invokes the SAS system and the SAS prompt with a line number and question mark, 1?, appears on your terminal. Type in the SAS statements and press ENTER. A statement is not complete until it is succeeded by a semicolon. SAS responds to statements as you enter them.
Following is an example of a SAS line-mode session saved from the UITS IBM RS/6000 computer called SP05. You may want to try it to have a feel for the SAS line mode. Enter the statements as given and press RETURN at the end of each line. If you notice a mistake in typing before pressing RETURN, just move the cursor backward and retype it. If SAS responds to you with an error, retype the whole line correctly.
NOTE: Copyright (c) 1999-2001 by SAS Institute Inc., Cary, NC, USA. NOTE: SAS (r) Proprietary Software Release 8.2 (TS2M0) Licensed to INDIANA UNIVERSITY, Site 0009093004. NOTE: This session is executing on the AIX 5.1 platform. The help with SAS computing, contact the UITS Stat/Math Center (855-4724; email email@example.com; URL http://www.indiana.edu/~statmath). NOTE: SAS initialization used: real time 0.21 seconds cpu time 0.05 seconds 1? data gender; 2? input id sex $ group test1 test2; 3? totscore=test1+test2; 4? cards; 5> 01 m 1 42 41 6> 02 m 1 41 37 7> 03 f 1 43 36 8> 04 f 1 34 32 9> 05 f 2 32 34 10> 06 f 2 30 35 11> 07 m 2 30 28 12> 08 m 2 26 30 13> ; NOTE: The data set WORK.GENDER has 8 observations and 6 variables. NOTE: DATA statement used: real time 2:30.17 cpu time 0.03 seconds 14? proc print; 15? var sex group totscore; 16? title 'Printing Variables'; 17? run; Printing Variables 1 14:12 Tuesday, August 15, 2000 Obs sex group totscore 1 m 1 83 2 m 1 78 3 f 1 79 4 f 1 66 5 f 2 66 6 f 2 67 7 m 2 58 8 m 2 56 NOTE: There were 8 observations read from the dataset WORK.GENDER. NOTE: PROCEDURE PRINT used: real time 48.46 seconds cpu time 0.10 seconds 18? endsas; NOTE: SAS Institute Inc., SAS Campus Drive, Cary, NC USA 27513-2414 NOTE: The SAS System used: real time 10:21.89 cpu time 1.06 seconds
You can enter any number of statements during line mode session. If you want to save the command lines you entered during an interactive session, enter the following lines before you end the session.
18? options spool; 19? filename mylog 'myprog.prg'; 20? proc printto log=mylog; 21? run; 22? %list; 23? endsas;
All SAS statements entered during the session will be stored in the default directory under the myprog.prg or a name you specify. This file will contain only the SAS statements. This file could be edited and later read into a SAS interactive session us ing the %INCLUDE command. You may also use the %INCLUDE command to read a SAS program file into a line-mode session. For example, to read a file, clas.sas, into the line-mode session, at the line mode prompt (e.g. 1?, 2?, 5?), type:
Now the entire file will be read into the session. If the filename has extension other than ".sas," you must enclose the full filename with extension in single quotes (e.g., clas.prg) in the INCLUDE statement. If there is an ENDSAS line at the end of your included file, SAS will, after execution, exit the job and take you to the system prompt.
If you want to save the .log and .lst files from the session to separate files in your directory, use the altlog and altprint options while invoking SAS in interactive mode.
Replace the log and the listing file with appropriate filenames. Your log and listing files will be saved in the default directory under the filenames specified. Refer to the SAS Companion for the UNIX Environment for details.
Once a SAS program file is created and saved you can run it non-interactively. Suppose you want to execute the command file, clas.sas, non-interactively. At the system prompt, type:
(Make sure that the data file, clas.dat, is accessible during the job.)
Note that the extension .sas is omitted since SAS automatically picks the file clas.sas for execution. Use the full name of the file if it does not have a .sas extension (e.g., sas clas.exp, sas clas.txt.). The cursor will move to the first column of the next line and rest there until the job is completed. You cannot use the terminal for any other job until the program is executed. When the job is completed the system prompt reappears. At this point, if your job was successfully executed, two new files (log and listing) will appear in your directory. The log file will have an extension of .log (e.g. clas.log). This file contains your program and any error message accompanying it. A second file with an extension of .lst (e.g. clas.lst) will be stored in your directory. This file contains the output of the procedures executed. If there are errors in the program file a listing file may or may not be created depending on the nature of the errors. It is always a good idea to check your .log file by displaying it on the terminal with the more or cat command or by printing it. If there are errors, edit the SAS program file and execute the program again.
One disadvantage of a non-interactive job is that it will tie up your terminal until the job is completed. However, if you want the terminal to be freed for other work while the job is running in the background, type:
You will see a process identification number (PID) for the background task and your terminal will be free for other computing while the job is running. Check the status of the job with the ps command from the system prompt. Once the job is executed, the log and listing file will be stored in the default directory. To kill a background process, use the kill command.
Batch Execution on the Research SP
Batch queues are available on all of the Research SP nodes (running IBM AIX). CPU-intensive jobs that require more CPU time to process must be submitted to the batch queue for execution.
You are required to use IBM's LoadLeveler batch queuing system for CPU-intensive jobs that require more than 20 minutes of CPU time. A type of queue is available for statistical jobs on the SP machines:
- stat - Jobs requiring up to 8-day of CPU time
To submit a SAS job to LoadLeveler, create a script file, e.g., sasjob1, with the following lines:
#@ requirements = (Feature == "sas") #@ initialdir = directory #@ error = filename #@ class = stat #@ queue sas inputfile
Replace "directory" with the directory where the command file is stored, although if you submit the job from the same directory as the command file, you don't need to specify the directory. Replace "filename" with the name of a file to which any script errors will be written. Finally, replace "inputfile" with the name of the SAS command file (e.g., clas.sas). The error/log file will be stored in the same directory in a file named sasjob1.err.
To submit the job, at the system prompt, type:
The output files will be stored in the directory specified in the script file. You may log out after submitting the job.
Next: SAS Data Sets
Up: Table of Contents
Prev: Writing a SAS Program: The PROC Step