This document provides three different examples of SAS programs and explains the different characteristics of each program.
A link to each sample program, with a brief description of its characteristics, is provided below. Additional links to the program text, the accompanying explanation, and any additional information, are provided in the Overview section of each sample program.
Following is the first of three examples of SAS programs. The program is written out first, and an explanation of the program is provided in the subsequent section.
DATA CLASS; INPUT NAME $ 1-8 SEX $ 10 AGE 12-13 HEIGHT 15-16 WEIGHT 18-22; CARDS; JOHN M 12 59 99.5 JAMES M 12 57 83.0 ALFRED M 14 69 112.5 ALICE F 13 56 84.0 PROC MEANS; VAR AGE HEIGHT WEIGHT; PROC PLOT; PLOT WEIGHT*HEIGHT; ENDSAS; ;
Following is the second of three examples of SAS programs. The program is written out first, and an explanation of the program is provided in the subsequent section.
If you run this sample program, you will see the contrast between the output layout and detail of the data summarizing SAS procedures named PROC MEANS and PROC UNIVARIATE.
DATA hatco ; options ls=79; options ps=60; INFILE '~dscbms/class/dsc8450/files/hatco'; INPUT X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 X11 X12 X13 X14; LABEL X1='DELIVERY SPEED' X2='PRICE LEVEL' X3='PRICE FLEXIBILITY' X4='MANUFACTURER IMAGE' X5='OVERALL SERVICE' X6='SALES FORCE IMAGE' X7='PRODUCT QUALITY' X8='SIZE OF FIRM' X9='USAGE LEVEL' X10='SATISFACTION LEVEL' X11='SPECIFICATION BUYING' X12='STRUCTURE OF PROCUREMENT' X13='TYPE OF INDUSTRY' X14='TYPE OF BUYING SITUATION'; Proc means; var x1-x14; PROC UNIVARIATE PLOT NORMAL; var x1-x7 x9;
Following is the third of three examples of SAS programs. The program is written out first, and an explanation of the program is provided in the subsequent section. Actually the LOG file is displayed, instead of a copy of the input program command file. The LOG file numbers each of the input program command lines and echos a description of the intermediate temporary files created and used by SAS. (Some output from the actual LOG file has been altered slightly.)
This sample program demonstrates data sets being created, variables being added, and the results further altered and handed off to other DATA steps and PROCs during a SAS program. This program demonstrates a very effective manner of generating two subsamples when the multivariate analysis requires an analysis sample (called firstsub below) and a holdout sample (called secndsub below). The original full sample is sorted according to arbitrarily assigned random numbers. Each sample value in the sorted data is assigned a sequence number which is kept in a special SAS variable named _n_. The data values are then assigned to the two samples according to the sequence number.
1 The SAS System 15:14 Wednesday, December 17, 1997 NOTE: Copyright (c) 1989-1996 by SAS Institute Inc., Cary, NC, USA. NOTE: SAS (r) Proprietary Software Release 6.11 TS040 Licensed to GEORGIA STATE UNIVERSITY, Site 0008840004. NOTE: SAS initialization used: real time 0.57 seconds cpu time 0.16 seconds 1 data in; 2 item+1; 3 input x1 $ @@; 4 cards;
Next are lines 5-9 from the program command file. They are NOT normally shown in the LOG file.1 2 3 4 5 6 7 8 9 10 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 1 2 3 4 5 6 7 8 9 10 b1 b2 b3 b4 b5 b6 b7 b8 b9 b10 1 2 3 4 5 6 7 8 9 10
NOTE: SAS went to a new line when INPUT statement reached past the end of a line. NOTE: The data set WORK.IN has 50 observations and 2 variables. 10 ; 11 12 proc print data = in; 13 title 'Total sample available'; 14 NOTE: The PROCEDURE PRINT printed page 1. 15 data withRan; 16 set in; 17 fraction = ranuni(74380); 18 NOTE: The data set WORK.WITHRAN has 50 observations and 3 variables. 2 The SAS System 15:14 Wednesday, December 17, 1997 19 proc sort data=withRan out=sorted; 20 by fraction; 21 NOTE: The data set WORK.SORTED has 50 observations and 3 variables. NOTE: PROCEDURE SORT used: real time 0.31 seconds cpu time 0.03 seconds 22 data firstsub; 23 set sorted; 24 if _n_ lt 15; 25 NOTE: The data set WORK.FIRSTSUB has 14 observations and 3 variables. 26 data secndsub; 27 set sorted; 28 if _n_ ge 15 and _n_ lt 29; 29 30 NOTE: The data set WORK.SECNDSUB has 14 observations and 3 variables. 31 proc print data = firstsub; 32 title 'Estimation sample'; NOTE: The PROCEDURE PRINT printed page 2. 33 proc print data = secndsub; 34 title 'Holdout sample'; NOTE: The PROCEDURE PRINT printed page 3. NOTE: The SAS System used: real time 2.15 seconds cpu time 0.45 seconds NOTE: SAS Institute Inc., SAS Campus Drive, Cary, NC USA 27513-2414
A Data Step
To check your understanding of sorting and data handling, continue this example by sorting the two samples data back into their original sequence. The program command file is saved as the filename random.