3 Example of SAS Program you need to know

2

SAS on Demand – Introduction

In this article, we are going to write about three different example of SAS program and explains the different characteristics of each program.

Before we proceed, we need to know what SaS is all about.

What is SaS Software?

SAS is a statistical software suite developed by SAS Institute for data management, advanced analytics, multivariate analysis, business intelligence, criminal investigation, and predictive analytics. SAS was developed at North Carolina State University from 1966 until 1976, when SAS Institute was incorporated.

(Wikipedia)

What is SaS used For?

SaS is a very useful software that is used to process Enormous array of statistical methods and algorithms, especially for advanced statistics and also Publish quality graphics with ODS.

It can also be used in business, medicine amd Large, active online community.

For Mac users, you can use SAS OnDemand for Academics. SAS OnDemand for Academics is a cloud-based service accessed through your browser, and is free to use however you have to create an account.

3 Example of SAS Program

SaS on Demand Program 1

This program reads data organized in fixed columns, from an inline source and uses two PROCs.

Following is the first of three example of SAS program. 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 DATA statement tells the computer that the data is coming from an inline source, SAS creates a temporary data file called WORK.CLASS .
  • The INPUT statement formats the variables for the computer.
    1. NAME: this is an alphanumeric variable, as indicated by the $. The variable NAME has been assigned columns 1-8.
    2. SEX: this is also an alphanumeric variable, and has been assigned column 10.
    3. AGE: this is a numeric variable, and has been assigned columns 12-13.
    4. HEIGHT: numeric variable, columns 15-16.
    5. WEIGHT: numeric variable, columns 18-22.
  • The CARDS statement informs the computer that the data is located in the next lines.

The PROCS

The first procedure, PROC MEANS, calculates the mean for every variable.

The second, PROC PLOT, plots the values for WEIGHT against HEIGHT.

SaS on Demand Program 2

This program reads data organized in columns separated by spaces, from an external file and uses two PROCs.

Following is the second of three example of SAS program. 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.

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

  • INFILE reads a data set, specified as ~dscbms/class/dsc8450/files/hatco . SAS creates a temporary data file called WORK.HATCO .
  • 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.
  • The program LABELs the output so that the display will be easier to understand.

The PROCS

PROC MEANS: computes the default summary statistics of all the variables x1 through x14.

PROC UNIVARIATE: computes more detailed summary statistics for the metric variables x1 through x7 and x9. The options PLOT and NORMAL produce special summaries.

SaS on Demand Program 3

Following is the third of three example of SAS program. 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.

SaS on Demand Program 3

This program reads data organized in columns separated by spaces, from within the program command file itself and uses several PROCs to choose two random samples.

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.

2 Comments
  1. Scopians says

    I loved as much as you will receive carried out right here The sketch is tasteful your authored subject matter stylish nonetheless you command get got an edginess over that you wish be delivering the following unwell unquestionably come further formerly again as exactly the same nearly very often inside case you shield this hike

  2. bonus di riferimento binance says

    Your article helped me a lot, is there any more related content? Thanks! https://www.binance.info/it/join?ref=V2H9AFPY

Leave a Reply

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Privacy & Cookies Policy
Follow us on Social Media