Return to Top Document

Overview

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.


Sample Program #1

Overview

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.

Program

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;
;

 

Explanation

 

The Data Step

 

The PROCS
  1. The first procedure, PROC MEANS, calculates the mean for every variable.
  2. The second, PROC PLOT, plots the values for WEIGHT against HEIGHT.


Sample Program #2

Overview

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.

Sample Program

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;

Explanation

The Data Step
  1. INFILE reads a data set, specified as ~dscbms/class/dsc8450/files/hatco . SAS creates a temporary data file called WORK.HATCO .
  2. The variables (X1, X2, X3, etc.) are read in as "list input," because the data (all numerical values, in this example) are stored in the file separated by spaces.
  3. The program LABELs the output so that the display will be easier to understand.
The PROCS
  1. PROC MEANS: computes the default summary statistics of all the variables x1 through x14.
  2. PROC UNIVARIATE: computes more detailed summary statistics for the metric variables x1 through x7 and x9. The options PLOT and NORMAL produce special summaries.


Sample Program #3

Overview

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.

Sample Program

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
       

Explanation

A Data Step
  1. SAS creates a temporary data file called WORK.IN .
  2. SAS creates a new variable named ITEM which has the value 1 for the first observation, and then increments by 1 for each additional observation. This variable will be used later to identify the original values.
  3. The variable values of one variable, X1, are read in as "list input," because the data are separated by spaces. The $ makes the values alphanumeric. The @@ tells SAS to continue to the same input line for read value of the variable, not to go to the next input line until the line is ended.
  4. CARDS reads the data set from lines within the inline source (the program command file).
  5. The data values are shown here, ten to a card.
  6. The data values are shown here, ten to a card.
  7. The data values are shown here, ten to a card.
  8. The data values are shown here, ten to a card.
  9. The data values are shown here, ten to a card.

    A Proc Print
  10. A semi-colon to end the data values.
  11. [Blank line]
  12. Produces a printed version of the original data.
  13. Produces a title.
  14. [Blank line] Next SAS declares the page number for the printed output.

    Another Data Step
  15. SAS creates a temporary data file called WORK.WITHRAN .
  16. The input data are WORK.IN .
  17. The variable fraction is created as a random fraction. The argument of RANUNI sets the random number seed. Using the same seed allows us to recreate the same sequence of random fractions.
  18. [Blank line] Next SAS reports the data description for WORK.WITHRAN .

    Proc Sort
  19. WORK.WITHRAN is sorted and WORK.SORTED is created.
  20. The observations are sorted according to the value of the variable fraction.
  21. [Blank line] Next SAS reports the data description for WORK.SORTED .

    Two more Data Steps
  22. SAS creates a temporary data file called WORK.FIRSTSUB .
  23. The input data are WORK.SORTED .
  24. The variable _n_ is a special variable which contains the observation's sequence number. The IF statement only outputs a data record if its condition is correct. Only the first 14 observations will be output to WORK.FIRSTSUB .
  25. [Blank line] Next SAS reports the data description for WORK.FIRSTSUB .
  26. SAS creates a temporary data file called WORK.SECNDSUB .
  27. The input data are WORK.SORTED .
  28. The variable _n_ is a special variable which contains the observation's sequence number. The IF statement only outputs a data record if its condition is correct. The next 14 observations will be output to WORK.SECNDSUB .
  29. [Blank line] Next SAS reports the data description for WORK.SECNDSUB .
  30. [Blank line]


    Two more Print Procs
  31. Produces a printed version of WORK.FIRSTSUB .
  32. Produces a title. Next SAS declares the page number for the printed output.
  33. Produces a printed version of WORK.SECNDSUB .
  34. Produces a title. Next SAS declares the page number for the printed output.

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.


Return to Top of Document