0% found this document useful (0 votes)
4 views

Week 1-PART 1-Review SAS Program Basics

This document serves as a lecture outline for a SAS programming course, covering the basics of SAS programming, including the SAS environment, syntax, datasets, and the creation of SAS datasets. It explains the structure and components of SAS programs, including DATA and PROC steps, and discusses temporary versus permanent datasets. Additionally, it provides examples of creating datasets and importing data from various sources.

Uploaded by

KinSparkin'
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Week 1-PART 1-Review SAS Program Basics

This document serves as a lecture outline for a SAS programming course, covering the basics of SAS programming, including the SAS environment, syntax, datasets, and the creation of SAS datasets. It explains the structure and components of SAS programs, including DATA and PROC steps, and discusses temporary versus permanent datasets. Additionally, it provides examples of creating datasets and importing data from various sources.

Uploaded by

KinSparkin'
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 37

Week 1-Reivew basics of

SAS Programming
PHEB 631: SAS PROGRAMMING FOR
EPIDEMIOLOGICAL RESEARCH
Xiaohui Xu, Ph.D.
Department of Epidemiology and Biostatistics
Part 1. Review SAS
program basics
Lecture Outlines
• Review SAS programming basics
• SAS environment
• SAS Syntax
• SAS Datasets (Temporary vs. Permanent)
• Creating SAS datasets
1.1 SAS Environment

4
1.1 SAS Environment
When you start the SAS Program, there are 5 basic SAS Windows or
Panels:
Editor: where you type programs for analyzing data
Log: where error messages and executed SAS commands are printed
Output: where the result of SAS programs are printed. There is one
more window linked to this window
Results: where you can see each section of your output
Explorer: where you can browse through your SAS programs where
SAS data is stored
5
1.1 SAS Environment

6
1.1 SAS Environment

SAS File Extension


SAS dataset .sas7bdat
SAS program .sas
SAS log .log
SAS output .lst
SAS transport .xpt
7
1.1 SAS Environment
Submit the program

8
1.2. SAS syntax-Structure And
Components Of SAS Programs
• The SAS program consists of a DATA step or a PROC step
or any combination of DATA and PROC steps
1.2.1 DATA steps
• DATA steps typically create or modify SAS data sets.
They can also be used to produce custom designed
reports. For example, you can use DATA steps to
• Put Your Data Into A SAS Data Set
• Compute Values
• Check For And Correct Errors In Your Data
• Produce New SAS Data Sets By Subsetting, Merging, And
Updating Existing Data Sets.
1.2.2 PROC (procedure) steps
• PROC (procedure) steps are pre-written routines that enable you
to analyze and process the data in a SAS data set and to present the
data in the form of a report. PROC steps sometimes create new SAS
data sets that contain the results of the procedure. PROC steps can
list, sort, and summarize data. For example, you can use PROC steps
to
• Create a report that lists the data
• Produce descriptive statistics
• Create a summary report
• Produce plots and charts.
Example- Proc Procedure (Proc
Means)
1.2.3 Boundary of SAS steps
• The beginning of a new step (DATA or PROC) implies the
end of the previous step.
• The RUN statement and the QUIT statement mark step
boundaries.
1.3 SAS statements
• SAS programs consist of SAS statements.
• Characteristics of SAS Programs
• All SAS statements must end with a semicolon (;).
• SAS statements typically begin with a SAS keyword. (Examples in the above
program include OPTIONS, TITLE, DATA, INPUT, DATALINES, RUN, PROC,
and VAR.)
• SAS programs can be freely formatted:
• SAS is not case sensitive; uppercase and lowercase letters are recognized as
the same, even for variable names.
• The words in SAS statements are separated by blanks or special characters
(e.g. =, +, or *).
1.3.1 free format of SAS
statement
 they can begin and end anywhere on a line
data clinic.admit2;
set clinic.admit;
run;
proc print data=clinic.admit2;
run;

 one statement can continue over several lines


proc
print
data=clinic.admit2;

 several statements can be on a line


data clinic.admit2;set clinic.admit;run;
Example
Create a new data set of “
Example: admit2” in the library “clinic”
data clinic.admit2; using the permanent data set
“admit” in the same library;
set clinic.admit;
run; Print out all records in
proc print data=clinic.admit2; the permanent dataset
run; “admit2” in the library
“clinic”
1.3.2 SAS comments
• Two ways:
• /*……………….*/
• *……………………;
1.4 SAS DATASETS
• Referencing SAS DATA Files
• You use a two-level name:
• libref.filename
• libref is the name of the SAS data library that contains the file
• filename is the name of the file itself. A period separates the
libref and filename.
1.4.1 SAS library
• A library is typically a group of SAS file in the same folder or directory.
• SAS default library : a temporary SAS library called Work.
• User specified library;
• LIBNAME libref 'SAS-library (directory of the folder)‘
• Libref: the library name which you assign; it is a “nickname” for the
aggregate storage location where your SAS files are stored.
• “sas-library”: the directory of a folder where you plan to save the data
• Example: Libname class “c:\week1”;
1.4.2 Temporary and Permanent
data files
• Temporary data files • Referencing temporary SAS file
• No library to be specified;
• Or use “WORK” Library
• When you close out the SAS
session, the temporary data
is deleted.
1.4.2 Temporary and Permanent
data files
• Permanent data files • Referencing Permanent SAS file
• To store files permanently in a SAS data library,
you specify a library name other than the
default library name Work.

• Example:
Libname clinic ‘c:\users\name\sasuser’;
Data clinic.admit2;
Set clinic.admit;
Weight=round(weight);
Run;
1.4.3 Rules for SAS names
• SAS names: • Rules:
• Library name • The length of a SAS
• Data set name name depends

• Variables
• The first character must be an
English letter (A, a, B, b, C, c, . . .
• Array name , Z, z,) or an underscore (_).
• …… • Blanks are not allowed.
• Special characters, except for the
underscore, are not allowed.
1.4.3 Rules for SAS names
Element Names Maximum Length in Bytes
Librefs (library names) 8
Data set name 32
Variable 32
Array names 32
1.5 Structure of SAS dataset
• A SAS data set is a file that consists of two parts
• a descriptor portion
• a data portion.
1.5.1 descriptor portion
• The descriptor portion of a SAS data set contains information
about the data set, including
•_ the name of the data set
•_ the date and time that the data set was created
•_ the number of observations
• _ the number of variables
1.5.2 Data portion
• The data portion of a SAS data set is a collection
of data values that are arranged in a rectangular
table.
• Rows: Rows (called observations) in the data set
are collections of data values that usually relate
to a single object. Individual records

• Columns: Columns (variables) in the data set
are collections of values that describe a particular
characteristic, names of variables
1.5.2.1 Variable attributes
• Name
• The same rule as data set name above
• Type
• A variable's type is either character or numeric.
• _ Character variables, such as Name, can contain any values.
• _ Numeric variables, such as Total score, can contain only numeric values (the digits 0 through
9, +, -, ., and E for scientific notation).
• Missing value
• For character variables such as Name, a blank represents a missing value.
• For numeric variables such as Age, a period represents a missing value.
1.5.2.1 Variable attributes
• Length
• A variable's length (the number of bytes used to store it) is
related to its type:
 Character variables can be up to 32,767 bytes long.
 All numeric variables have a default length of 8.
• Format
• Formats are variable attributes that affect the way data values
are written
• Example: to display the value 1234 as $1234.00 in a report,
you can use the DOLLAR8.2 format
1.5.2.1 Variable attributes
• Informat
• informats read data values in certain forms into standard SAS values.
• For example, the numeric value $1,234.00 contains two special characters, a dollar
sign ($) and a comma (,). You can use an informat to read the value while removing
the dollar sign and comma, and then store the resulting value as a standard numeric
value.
• Label
• A variable can have a label, which consists of descriptive text up to 256 characters
long
1.6 Creating SAS datasets
1. Direct Entry
2. Reading external data into SAS: Text File: txt, Excel File:
xls, xlsx, csv
3. Reading SAS Transport File: xpt
1.6.1 Direct entry
2.1 Create a dataset “TEST”: Create a temporary data
SAS Program: set “TEST”
DATA TEST;
INPUT SUBJECT 1-2 GENDER $ 4 EXAM1 6-8 EXAM2 10-12
HW_GRADE $ 14;
DATALINES;
10 M 80 84 A
7 M 85 89 A DATALINES Input statement show what to call
4 F 90 86 B statement show the variables and where to find
20 M 82 85 B that the DATA them on the data line; Among
25 F 94 94 A statement end them, “GENDER” and
14 F 88 84 C here and below “HW_GRADE” are character
; are the actual variables
data
Run
1.6.1 Direct entry
2.2. Example –Enhancing Data Step program

SAS Program:

DATA EXAMPLE; Create a variable of “FINAL”


INPUT SUBJECT GENDER $ EXAM1 EXAM2 BY assigning the average of
HW_GRADE $; EXAM1 and EXAM2
FINAL = (EXAM1 + EXAM2) / 2;
IF FINAL GE 0 AND FINAL LT 65 THEN GRADE='F';
ELSE IF FINAL GE 65 AND FINAL LT 75 THEN GRADE='C';
ELSE IF FINAL GE 75 AND FINAL LT 85 THEN GRADE='B';
ELSE IF FINAL GE 85 THEN GRADE='A';
DATALINES;
10 M 80 84 A
7 M 85 89 A
The Logical statements are
used to compute a new
variable “GRADE” based on
“FINAL”
1.6.2 Creating Permanent Dataset in SAS
Tab-delimited text data (Data-infile)

Libname mydata “C:\Users\taehyunroh\


Desktop\mydata”;
data mydata.example1;
infile "C:\Users\taehyunroh\Desktop\mydata\
example1.txt";
input mpg weight price;
run;
34
Practice: Ex01.txt
DATA grade;
infile "...\ex01.txt";
InPuT subject gender $ exam1 exam2 hwgrade $;
LABEL exam1 = "Exam 1 grade";
FORMAT exam2 4.2;

RUN;

PROC PRINT data=grade;


var subject gender; * print student ID and gender;
run;
1.6.2 Creating Permanent Dataset in
SAS : Proc import
Libname mydata “C:\Users\Desktop\mydata”;
PROC IMPORT OUT=mydata.sample *Output SAS dataset with either one or two-level SAS name(library &Member
name). If the specified dataset doesnot exist Proc import creates it. If it is a one-level name then proc import uses
the WORK library;
DATAFILE="C:\Users\Desktop\mydata\Data.xlsx“ *Complete path and filename of the input PC file;
DBMS=xlsx REPLACE; *DBMS specifies the type of data to import;
Example: delimited file = comma-separated values (.csv), tab-delimited values(.txt); Excel=.xls, Microsoft access
database = .mdb; lotus =.wk1/3/4(spreadsheet)

RANGE=
SHEET=“Sheet1$”;
GETNAMES=YES;
DATAROW=2;
Run;

36
1.6.3. Creating Permanent Dataset in SAS
xpt file (e.g., NHANES datasets)

You just need to change parts in red box.


libname mydata "C:\Users\taehyunroh\mydata\";
libname xptfile xport "C:\Users\taehyunroh\mydata\
DEMO_I.xpt";
proc copy inlib=xptfile outlib=mydata;
run;

37

You might also like