Docsity

SAS Crash Course: A Quick Guide to Using SAS for Statistical Analysis - Prof. John R. Stev, Study notes of Statistics

Toggle sidebar
bg1
Statistics 5100 SAS Crash Course
To open SAS in AgSci 119: Start →Programs →Reserve →SAS →SAS 9.1
SAS is a software package used by many companies and researchers. We will focus in this course
on the statistical tools available in SAS. This document will give you a quick crash course in some
of the basics of SAS operation. SAS has four main windows:
•Editor: type and run commands here; “color” matters; .sas files open here
•Log: tells what was “done”; helps with debugging; “color” matters
•Output: see results
•Graph: shows “nice” graphs [only created by certain commands]
To “run” a SAS program (or a highlighted section of SAS code), click on the “running man” icon,
or hit F8.
Read in Data:
•By “hand”: data a1;
input x y;
cards;
1 2
1 4
2 3
3 2
;
run;
•Infile statement:
C:\folder\datafile.txt
1 2
1 4
2 3
3 2
SAS code
data a1;
infile ’C:\folder\datafile.txt’;
input x y;
run;
•“Import” facilities: for “specially formatted” data files; more later.
Necessary components to run a program:
•semi-colon at the end of every line (except for datalines)
•a data statement that names your data set (unless you import the data set)
•input statement (unless you import the data set)
•at least one space between each word or statement
•a run statement
pf3
pf4

Partial preview of the text

Download SAS Crash Course: A Quick Guide to Using SAS for Statistical Analysis - Prof. John R. Stev and more Study notes Statistics in PDF only on Docsity!

Statistics 5100 SAS Crash Course

To open SAS in AgSci 119: Start → Programs → Reserve → SAS → SAS 9.

SAS is a software package used by many companies and researchers. We will focus in this course on the statistical tools available in SAS. This document will give you a quick crash course in some of the basics of SAS operation. SAS has four main windows:

  • Editor: type and run commands here; “color” matters; .sas files open here
  • Log: tells what was “done”; helps with debugging; “color” matters
  • Output: see results
  • Graph: shows “nice” graphs [only created by certain commands]

To “run” a SAS program (or a highlighted section of SAS code), click on the “running man” icon, or hit F8.

Read in Data:

  • By “hand”: data a1; input x y; cards; 1 2 1 4 2 3 3 2 ; run;
  • Infile statement: C:\folder\datafile.txt

1 2 1 4 2 3 3 2

SAS code

data a1; infile ’C:\folder\datafile.txt’; input x y; run;

  • “Import” facilities: for “specially formatted” data files; more later.

Necessary components to run a program:

  • semi-colon at the end of every line (except for datalines)
  • a data statement that names your data set (unless you import the data set)
  • input statement (unless you import the data set)
  • at least one space between each word or statement
  • a run statement

Mathematical Operators: Function Operator Example addition + x+y subtraction - x-y multiplication * xy division / x/y power ** or ^ x* log log log(x) exponential exp exp(x)

Logical Operator Example equal = or eq if x = 1 unequal <> or ne if x <> 1 less than < or lt if x < 1 less than or equal to <= or le if x <= 1 greater than > or gt if x > 1 greater than or equal to >= or ge if x >= 1

Sample SAS code to create and view a new data set:

data a2; set a1; xy = xy; xsq = x*2; xeq1 = 0; if x = 1 then xeq1=1; run; proc print data=a2; var x y xeq1 xy xsq; title1 ’New Data Set’; run;

New Data Set

Obs x y xeq1 xy xsq

Sample SAS code to only keep certain observations in a new data set:

data a3; set a2; if y < 3.5; /* Default ’then’ is keep / run; / Same as: */ data a3; set a2; if y >= 3.5 then delete; run; proc print data=a3; var x y xeq1; title1 ’Smaller Set’; run;

Smaller Set

Obs x y xeq

Miscellaneous SAS Notes

  • Help in SAS: The help facility in SAS (drop-down menu or the question-mark-book icon) can be useful if you have a good idea of what you want to do. Some examples: - See appropriate syntax and possible options for specific procedure - See examples of SAS code for specific analyses or graphics - Learn more about what a specific procedure option does (see title1 below).
  • Code Continuity: Code can be written across lines; SAS only looks for semicolons to break up code, except for data lines. To read in data “continuously”, use @@.
  • Missing Values: SAS procedures will completely ignore an observation if one of the called variables is missing; to code a value as “missing” in SAS, use the period (.) character.
  • “Strings”: Read in character variables with $ after the name in the input line.
  • Comment Lines: To comment out a line up to the next semi-colon, put an asterisk * before it. To comment out an entire section, start it with /* and end it with */
  • Output Options: You can make the SAS output window be a little more friendly with judicious use of the options statement.
  • Selective Output: SAS will usually give you more than you want, so you’ll need to know what you want in order to do anything useful with the output.
  • Save Code: Save your code (from the Editor window) as a .sas file in whatever location (thumb drive?) you choose; double-click on file icon to open SAS; run to recreate all results
  • Example and portions of output:

options ls=80 nodate pageno=1 formdlim="~";

data a1; input x y z $ @@; cards; 1 2 alpha 1 4. 2 3 gamma 3. delta ; run; proc print data=a1;

  • var y x z; title1 "Data Set"; run;

proc means data=a1; var y; title1 "Means Output"; run; */

proc print data=a1; var y x z; title2 "Subtitle"; run;

Data Set 1

Obs x y z

1 1 2 alpha 2 1 4 3 2 3 gamma 4 3. delta

Data Set 2 Subtitle

Obs y x z

1 2 1 alpha 2 4 1 3 3 2 gamma

  1. 3 delta