Download - SAS Training Session 1
-
7/29/2019 SAS Training Session 1
1/38
SAS Training Session 1
Getting Started with SAS
Sun Li
Centre for Academic Computing
mailto:[email protected]:[email protected] -
7/29/2019 SAS Training Session 1
2/38
Outline
Section 1: Getting to know SAS See an overview of SAS
Explore the SAS workspace
Work with SAS data sets
Create and run SAS programs Work with SAS dates and times
Section 2: Exploring sample codes on SAS DATA steps
Read SAS data sets from raw data format or existing SAS data set
Manage data
Use SQL to query & combine SAS data sets
-
7/29/2019 SAS Training Session 1
3/38
Software Installation
Faculty:
CAC website: https://mercury.smu.edu.sg/research_portal/index.asp
=> Software Requisition
Postgraduate students
Have your supervisors consent. Please ask your supervisor to:
Drop us a consent email to [email protected], stating the reason
for request and students detail.
Or, directly raise Software Requisition request from CAC website ,stating the reason and students detail.
CAC Support Engineer will contact you once we receive
your request.
https://mercury.smu.edu.sg/research_portal/index.aspmailto:[email protected]://mercury.smu.edu.sg/research_portal/index.asphttps://mercury.smu.edu.sg/research_portal/index.aspmailto:[email protected]://mercury.smu.edu.sg/research_portal/index.asp -
7/29/2019 SAS Training Session 1
4/38
See an overview of SAS
Data accessYou can read raw data in almost any format, from any kind of file, including variable-length
records, binary files, free-formatted dataeven files with messy or missing data.
Managing your dataAfter you've accessed your data, you can use the SAS programming language to manipulate it
any way you choose. For example, you can format your data, create variables, use functions to create data
values, subset data, perform conditional processing, merge data sources, etc.
Analysis & PresentationSAS provides a powerful data analysis tools. You can create graphs, generate a variety of
statistics and models. For reporting and displaying results, SAS gives you an almost limitless number of
visually appealing output formats: HTML format, PDF, PostScipt, RTF and colorful graphs.
-
7/29/2019 SAS Training Session 1
5/38
Explore the SAS workspace
-
7/29/2019 SAS Training Session 1
6/38
Explore the SAS workspace
Solution and Tool menus: A set of ready-to-use solutions,
applications, and tools.
Help menu:
-
7/29/2019 SAS Training Session 1
7/38
Work with SAS data sets
The most commonly used SAS files:
SAS data set: *. sas7bdat
SAS program file: *. sas
SAS list file/ procedure output: *. lst
SAS log file: *. log
Spreadsheet data format:
-
7/29/2019 SAS Training Session 1
8/38
Work with SAS data sets
SAS library: All SAS files are stored in SAS library. Its a group of files in the same folder or directory.
To access a library, you assign it a name (also known as a libref, or library reference).
librefmust be 8 characters or less.
The full reference to a SAS file is libref.filename.
-
7/29/2019 SAS Training Session 1
9/38
Work with SAS data sets
Pre-assigned SAS libraries: Sashelp: a permanent library that contains sample data and other files that control how
SAS works at your site. This is a read-only library.
Sasuser: a permanent library that contains SAS files in the Profile catalog that store yourpersonal settings.
Work: a temporary library for files that do not need to be saved from session to session.
Manage files in a SAS library: Open: double-click to open library and file.
View data property: right-click and select Properties. Copy files: click and drag under Show Tree mode. With the Explorerwindow active, goto
menu View-> Show Tree.
View column format: open SAS data file and right-click column heading, and selectColumn Attributes.
-
7/29/2019 SAS Training Session 1
10/38
Work with SAS data sets
SAS data formats & informats: Formats are variable attributes that affect the way data values are written. General form
of a format is:
format.
$ indicates a charater format; format is a format name;
wis total width including decimal places and special characters;
Period . requires delimiter;
dis the number of decimal places.
Example:
Data 12234.21 KATHY 12,234.21 $12,234.21
Format 8.2 $5. COMMA9.2 DOLLAR10.2
An informat (input format) is the instruction that specifies how SAS reads raw data. Itsgeneral form is the same as SAS formats.
-
7/29/2019 SAS Training Session 1
11/38
Create and run SAS programs
Three major steps:
DATA step Enter, manage and manipulate data
Merge data, recode, label and format data Convert non-SAS data sets into SAS data sets
PROC step Conduct statistical analysis & other procedures
Output data into tables and graphs
Convert data into other formats
Macro Rationalize the repetitive sections of programs
Undergo the preprocessing
-
7/29/2019 SAS Training Session 1
12/38
Create and run SAS programs
Components of SAS programs: SAS statements:
begin with an identifying keyword
end with a semicolon.
SAS steps begin with a DATA statement or a PROC
statement.
end with a RUN statement or a QUITstatementor the beginning of another step.
SAS ignores spaces, carriage returns, or extra lines.
To write a comment: Begin with an asterisk * and end with; semicolon
Begin with/* and end with */
SAS statements and commands are not case sensitive.
SAS keywords are color-coded.
Question:How many SAS statements ?
How many SAS steps ?
-
7/29/2019 SAS Training Session 1
13/38
Create and run SAS programs
Submit SAS programs and view
output: Select program lines and click menu Run -> Submit.
Program output will be displayed in the Output windowif there is any output. For graphs, a Graph window will
pop in.
Save SAS programs and
output: With the Editor window / Output window active, click
menu File -> Save / Save as.
-
7/29/2019 SAS Training Session 1
14/38
Create and run SAS programs
OperatorsOperator Operation Example
** Exponentiation x**2
x**.5
* Multiplication x*y
x*12/ Division year/12
+ Addition dose1+dose2
- Subtraction x-y
Operator Alter. Meaning
= eq Equal
^=
~=
ne Not equal
> gt Greater than
< lt Less than
>= ge Greater than orequal to
-
7/29/2019 SAS Training Session 1
15/38
-
7/29/2019 SAS Training Session 1
16/38
Work with SAS dates and times
SAS datetime value: A value representing the number of seconds between January 1,
1960 and an hour/minute/second within a specified date.
Common Informats for Dates:
July 4, 1776
11:30:23
Jan 1, 1960
midnight
April 22, 1989
16:10:45
-5790400177 0 92488384
Date expression SAS date informat SAS date value
02Jan2000 DATEw. 14611
02Jan00 DATEw. 14611
01/02/2000 MMDDYYw. 14611
January 02, 2000 WORDDATEw. 14611
Sunday, January 02, 2000 WEEKDATEw. 14611
-
7/29/2019 SAS Training Session 1
17/38
-
7/29/2019 SAS Training Session 1
18/38
Work with SAS dates and times
However, you can override the default and change the value of byspecifying the YEARCUTOFF= option in the OPTIONS statement to change .
options yearcutoff=1950;
Date expression Yearcutoff=1920
(by default)
Yearcutoff=1950
12/07/41 12/07/1941 12/07/2041
18Dec15 18Dec2015 18Dec2015
04/15/30 04/15/1930 04/15/2030
15Apr95 15Apr1995 15Apr1995
-
7/29/2019 SAS Training Session 1
19/38
-
7/29/2019 SAS Training Session 1
20/38
Section 1: Getting to know SAS
We have learnt in Section 1:
SAS windows
SAS files
SAS library
SAS data formats & informats
SAS program steps & statements
SAS operators
SAS date & time values
-
7/29/2019 SAS Training Session 1
21/38
-
7/29/2019 SAS Training Session 1
22/38
Create SAS data sets
Create a SAS library: To refer a SAS file in a permanent library, use two-level name: libref.filename.
To refer a SAS file in a Work library (temporary library), we can omit the libref workand write
filename directory.
Example: libname sas1 E:\lsun';
-
7/29/2019 SAS Training Session 1
23/38
Create SAS data sets
Read external raw data:Standard and non-standard format data:
Example of standard numeric data: 15 -15 15.4 +1.23 1.23E3
Example of nonstandard numeric data: 5,000 $1,000.00 10/31/02 (23)
Three methods to read data into SAS as a SAS data set:
Formatted input:
standard and nonstandard character and numeric data
calendar style date values and convert them to SAS date values
Column input: data in fixed columns
standard character and numeric data
Point-and-click: SAS Import Wizard
Click menu File -> Import data
Microsoft Excel *.xls, Comma separated values *.csv, Tab limited file *.txt, some database data sets.
-
7/29/2019 SAS Training Session 1
24/38
Create SAS data sets
Example:sp1.txt
0,70,4,1,1,general,4feb1989
1,121,4,2,1,vocati,11nov1989
0,86,4,3,1,general,22oct1991
0,141,4,3,1,vocati,4feb1993==============================
** read standard and nonstandard data;
DATA sas1.sp1;infile E:\lsun\sp1.txt' dlm=',' ;input gender id race ses schtyp prgtype $ date: date9.;RUN;
DATA sas1.sp2;infile E:\lsun\sp2.txt' ;input name $ 1-20 age 21-22 gender $ 23;RUN;
infile: identifies an external raw data file to read.
input: lists variable names in the input file.
sp2.txt
Billy Ray Washington 12M
Sally Hamrick 19M
Robert J. Castle 45M
Glenda Smith-Neil 21F
Codebook for sp2.txt:
Variable name Column number
Name 1-20
Age 21-22
Gender 23
-
7/29/2019 SAS Training Session 1
25/38
Create SAS data sets
Create SAS data sets from direct input:** create SAS data set from direct input;DATA quarter1;length Department $ 7 Site $ 8;input Department Site Quarter Sales;
datalines;Parts Sydney 1 4043.97Parts Atlanta 1 6225.26Parts Paris 1 3543.97Repairs Sydney 1 5592.82Repairs Atlanta 1 9210.21Repairs Paris 1 8591.98Tools Sydney 1 1775.74Tools Atlanta 1 2424.19Tools Paris 1 5914.25;RUN;PROC PRINT data=quarter1;RUN;
length: indicates length of variables.
datalines: indicates internal data.
-
7/29/2019 SAS Training Session 1
26/38
Manage SAS data sets
Sort and Print data:
** sort and print data ;PROC CONTENTS data=sas1.prdsale;RUN;
PROC CONTENTS nods data=work._all_;RUN;
PROC PRINT data=sas1.prdsale(firstobs=100 obs=200);RUN;
PROC PRINT data=sas1.prdsale;where year=1993;var actual predict product year;RUN;
PROC SORT data=sas1.prdsale;by product descending actual;RUN;PROC SORT data=sas1.prdsaleout=sortsale;by product;RUN;
PROC CONTENTS: displays thedescriptor portion of a SAS data set
PROC PRINT: displays the data portionof a SAS data set.
PROC SORT: sorts a SAS data set
where: selects observations that meet acertain condition.
var: gives the variable list;
-
7/29/2019 SAS Training Session 1
27/38
Manage SAS data sets
Subset data: SET statement:
** writing to a single SAS data set;DATA prdsale;set sashelp.prdsale;RUN;PROC PRINT data=prdsale (obs=10); RUN;DATA lowsale;set prdsale;if predict < 500;RUN;PROC PRINT data=lowsale (obs=10); RUN;
set: reads data set.
Keep /drop: keeps/dropsnamed variables.
if-then-else: executes astatement on if thecondition is true.
DATA diffsale;set prdsale;diff=actual-predict;drop actual predict;RUN;
DATA sale_cat;length cat $ 8;set prdsale;if 0
-
7/29/2019 SAS Training Session 1
28/38
Manage SAS data sets
DATA product_sample;do i=1 to 1440 by 10;set prdsale point=i;if _error_ then abort;output;end;stop;RUN;PROC PRINT data=product_sample (obs=10); RUN;
do-end: generates an iterative DOloop process.
point-stop: accesses the observationdirectly.
abort: goes out of loop;
output: invokes an explicit output
** Writing to multiple SAS data sets;DATA lperf mperf hperf;drop cat;
set sale_cat;if cat='low' then output lperf;else if cat='moderate' then output mperf;else output hperf;RUN;PROC PRINT data=lperf (obs=10);PROC PRINT data=mperf (obs=10);PROC PRINT data=hperf (obs=10);RUN;
-
7/29/2019 SAS Training Session 1
29/38
-
7/29/2019 SAS Training Session 1
30/38
Manage SAS data sets
Data conversion:
**character-to-numeric conversion;DATA conversion1;cvar1='32000';cvar2='32,000';cvar3='03may2008';cvar4='050308';nvar1=input(cvar1, 5.);nvar2=input(cvar2, comma6.);nvar3=input(cvar3, date9.);nvar4=input(cvar4, mmddyy6.);RUN;PROC CONTENTS data=conversion1; RUN;PROC PRINT data=conversion1; RUN;
input: explicitly convert charactervalues to numeric values.
NumVar=INPUT(source, informat);
SAS auto converts a character value to a
numeric value when the character value
is used in a numeric context such as an
arithmetic operation or a function taking
numeric arguments.
-
7/29/2019 SAS Training Session 1
31/38
Manage SAS data sets
**numeric-to-character conversion;DATA conversion2;nvar1=614;nvar2=55000;nvar3=366;cvar1=put(nvar1, 3.);
cvar2=put(nvar2, dollar7.);cvar3=put(nvar3, date9.);RUN;PROC CONTENTS data=conversion2; RUN;PROC PRINT data=conversion2; RUN;
put: explicitly convert numericvalues to character values.
CharVar=PUT(source, format);
SAS auto converts a numeric value to a
character value when the numeric value
is used in a character context such as an
assignment to a character variable or a
function that accepts character
arguments.Note:
Numeric formats right-align the results.
Character formats left-align the results.
-
7/29/2019 SAS Training Session 1
32/38
Manage SAS data sets
PROC SQL -- query data:Structured Query Language (SQL) is a standardized, widely used language
that retrieves and updates data in tables and views based on those tables.
DATA payroll;input IdNumber $ 1-4 Sex $ 6 Jobcode $ 8-10 Salary 12-16@18 Birth date7. @26 Hired date7.;datalines;1009 M TA1 28880 02MAR59 26MAR921017 M TA3 40858 28DEC57 16OCT811036 F TA3 39392 19MAY65 23OCT841037 F TA1 28558 10APR64 13SEP921038 F TA1 26533 09NOV69 23NOV911050 M ME2 35167 14JUL63 24AUG861065 M ME2 35090 26JAN44 07JAN871076 M PT1 66558 14OCT55 03OCT911094 M FA1 22268 02APR70 17APR911100 M BCK 25004 01DEC60 07MAY88;RUN;
-
7/29/2019 SAS Training Session 1
33/38
-
7/29/2019 SAS Training Session 1
34/38
-
7/29/2019 SAS Training Session 1
35/38
Manage SAS data sets
PROC SQL;create table oildata asselect p.country, barrelsperday 'Production, barrels 'Reserves'
from sas1.oilprod as p, sas1.oilrsrvs as rwhere p.country = r.countryorder by barrelsperday desc;
QUIT;The SELECTclause selects variables with labels assigned. Because the Country
columns are common to both tables, you qualify them in the SELECTand WHERE
clauses with the table alias p that is assigned in the FROMclause. You could also
qualify the columns by prefixing the column names with the table names. TheWHEREclause selects only rows that have matching values for Country.
The ORDER BYclause orders the output in descending order by the BarrelsPerDay
column. Because the column name appears only in the OilProd table, you don't have
to qualify the column name.
-
7/29/2019 SAS Training Session 1
36/38
Manage SAS data sets
QUIZ 3
See the file QUIZ-SAS1.pdf.
-
7/29/2019 SAS Training Session 1
37/38
Resources and books
You may find the following books helpful: Combining and Modifying SAS Data Sets: Examples
The Little SAS Book: A Primer, Second Edition
SAS SQL Procedure User's Guide
Step-by-Step Programming with Base SAS Software
CAC statistical consultation support:
CAC statistical WIKI page:
http://research2.smu.edu.sg/CAC/StatisticalComputing/Wiki/SAS.aspx
Statistical consultation service: [email protected]
http://research2.smu.edu.sg/CAC/StatisticalComputing/Wiki/SAS.aspxmailto:[email protected]:[email protected]://research2.smu.edu.sg/CAC/StatisticalComputing/Wiki/SAS.aspx -
7/29/2019 SAS Training Session 1
38/38