sas fundamental
TRANSCRIPT
An Introduction An Introduction to to
SAS SAS ProgrammingProgramming
Last Updated : 29 June, 2004
Center of Excellence
Data Warehousing
AgendaAgenda
Working with SAS EnvironmentSAS Programming BasicsEditing and Debugging SAS Programs Setting up the SAS SessionCreating Basic Reports and Statistics
ObjectivesObjectives
Learn toWork with and manage your SAS windows Create SAS libraries Explore and manage SAS files Enter and submit SAS programs Create and use file shortcuts.
Explore WindowExplore Window
User toCreate new libraries and SAS files Open any SAS file Consolidation of
disparate information sourcesPerform most file management tasks such
as moving, copying, and deleting files Faster time-to-market for products and services
Create shortcuts to non-SAS files.
Features of Program EditorFeatures of Program Editor
Drag and drop SAS Programs on this window
Specify the number of lines to submit at a time
Recall submitted statements Save contents automatically Clear contents Turn line numbers on and offUse the command line or menus save your program.
Other Windows’ FeaturesOther Windows’ Features
Logview messages about your SAS session and
any SAS programs you submit
Outputbrowse output from SAS programs
Resultnavigate and manage output from SAS
programs view, save, and print individual items of output
Working in SAS Working in SAS
Objectives The structure and components of SAS
programsThe steps involved in processing SAS programs The structure and components of SAS data
setsThe two types of SAS data setsSAS libraries and the types of SAS files that
they contain Temporary and permanent SAS libraries.
Layout of SAS ProgramsLayout of SAS Programs
SAS Programs used to access, analyze, manage and present data
SAS Statements Case insensitiveFree FormatEnds with a semicolon
Styles for Comment - *statement; or /* …*/
Parts of SAS ProgramsParts of SAS Programs
Data Step TO CREATE DATA SETS
put your data into a SAS data set compute the values for new variables check for and correct errors in your data produce new SAS data sets by subsetting, merging, and
updating existing data sets.
Proc Step PROCESS DATA SETS
print a report produce descriptive statistics create a tabular report produce plots and charts.
SAS NAMES (VARIABLES & FILE NAMES)SAS NAMES (VARIABLES & FILE NAMES)NAMES
1 TO 32 CHARACTERS IN LENGTHSTART WITH LETTER (A THRU Z) AND _CAN BE A COMBINATION OF NUMBERS/LETTERS
VARIABLESCHARACTERNUMERIC
FILENAMES2 LEVELLIB - WORK/PERMANENT
Editing SAS ProgramsEditing SAS Programs
the Program Editor window the Enhanced Editor the SAS Notepad window the host editor of your choice
Processing SAS ProgramsProcessing SAS Programs
When you submit a SAS program, SAS software reads the statements and checks them for errors.
When it encounters a DATA, PROC, or RUN statement, SAS software stops reading statements and executes the current step in the program. In our sample program, each step ends with a RUN statement.
Example:-Data Libname.New datasetname;Set Libname.Old datasetname;run;
Overview of SAS Data SetsOverview of SAS Data Sets
Conceptually, a SAS data set is a file consisting of two parts: a descriptor portion and a data portion. Some SAS data sets also contain an index, which enables SAS software to locate records in the data set.
Descriptor Portion name of the data set date and time the data set was created number of observations number of variables. each variable's name, type, length, format,
informat, and label.
Data PortionData Portion
Data PortionTabular Arrangement of dataContains Observations and VariablesVariables can be either Character or numericCharacter variables can be up to 32K longNumeric Values stored as floating point
variables have a default length of 8 bytesSAS can handle up to 32,767 variables
Naming RulesNaming Rules
SAS data set names mustbe 1 to 32 characters in length begin with a letter (A-Z, including mixed case
characters) or an underscore (_) continue with any combination of numbers,
letters, or underscores.
Example:-Payroll Budget1995_1997 _Students
READING RAW DATAREADING RAW DATA
DATA STARTOUTPUT DATA SET
INFILEPOINTER TO EXT FILE
INPUTHOW TO READ INPUT @1 DATE $9. @10 BOARDED 3.;
FLAT FILE VARIATIONS (DELIMITERS, LINE POINTER, LINE HOLD SPECIFIERS)
Variable FormatsVariable Formats
SAS Format & Informat
Data Value
SAS Data Value
Formatted Value
SAS FilesSAS Files
TableViewCatalogMDDB
SAS files are stored in a library.
Creating SAS Data SetsCreating SAS Data Sets
ScheduleCreating Data Sets From Raw FilesSubsetting DataSAS FunctionsReading Fixed Fields, Free Format filesReading from files with multiple rows in a
single line Reading from files with a single row in multiple
linesUnderstanding Data Step ProcessingReading Hierarchical FilesReading Variable Length Records
Base SASBase SAS
data access management analysis presentation
ExampleExample
With SAS, we can join Oracle data on a mainframe computer with an existing SAS data set, create new variables (columns), and produce an interactive graph on your PC.
Types of Files We Can Access Types of Files We Can Access
SAS/ACCESS provides access to these types of files:
Relational databases
DB2 under OS/390
Informix SYBASE
Oracle Rdb DB2 under VM ODBC
ORACLE MS SQL Server DB2 under UNIX or PC
CA-OpenIngres Teradata OLE DB
Contd…Contd…
Non-relational databases and other data sources ADABAS PC File Formats CA-IDMS
IMS-DL/I SYSTEM 2000 software
CA-DATACOM/DB
Contd…Contd…
Enterprise Resource Planning (ERP) systems Baan
PeopleSoft
R/3 SAP BW
Managing Data Managing Data
After we have accessed relevant data, we can use the SAS programming language to manipulate it any way we choose.
For example, we can For example, we can
format the data
create variables (columns)
use operators to evaluate data values
use functions to create and recode data values
subset data
perform conditional processing
merge a wide range of data sources
create, retrieve, and update database information.
Analyzing Data and Presenting Information Analyzing Data and Presenting Information Once our data is in shape, we can use SAS
to analyze data and produce reports. Our SAS output can range from a simple listing of a data set to customized reports of complex relationships.
.
AnalysisAnalysis
Base SAS provides powerful data analysis tools. For example, we can
produce tables, frequency counts, and cross-tabulation tables
create a variety of charts and plots
compute a variety of descriptive statistics, including the mean, sum, variance, standard deviation and more
compute correlations and other measures of association, as well as multi-way cross-tabulations and inferential statistics.
Presentation Presentation
For reporting and displaying analytical results, SAS gives us an almost limitless number of visually appealing output formats, such as
an array of markup languages including HTML4 and XML
output that is formatted for a high-resolution printer, such PostScript, PDF, and PCL files
color graphs that you can make interactive using ActiveX controls or Java applets.
RTF
Explore the SAS workspace Explore the SAS workspace
SAS is designed to be easy to use. It provides windows for accomplishing all the basic SAS tasks we need to do. When you first start SAS, the five main SAS windows open: the Explorer, Results, Program Editor or Editor, Log, and Output windows.
View the Explorer window View the Explorer window
In the Explorer window, we can view and manage our SAS files and create shortcuts to files that are not formatted by SAS. Use this window to
create new SAS libraries and SAS files open any SAS file perform most file management tasks such as moving, copying, and deleting files create file shortcuts.
View the Editor window View the Editor window
We can use either of these windows to enter, edit, and submit SAS programs:
the Program Editor window, which is available on all SAS platforms the Editor window, which is available only in the Windows operating environment.
View the Log window View the Log window
The Log window displays messages about our SAS session and any SAS programs that we submit.
View the Output windowView the Output window
The Output window displays the output from SAS programs that we submit. It automatically opens or moves to the front of our display when we create output.
Contd…..Contd…..
If we create HTML output, we can view it in the Results Viewer window, which is the internal browser for SAS.
View the Results windowView the Results window
The Results window helps us to navigate and manage output from SAS programs that we submit. We can view, save, and print individual
items ofoutput. The Results window is empty until we submit a SAS program that creates output. Then it opens or moves to the front of our display.
View the Solutions and Tools menus View the Solutions and Tools menus Along with windows for working with our SAS files and SAS programs, SAS provides a set of ready-to-use solutions, applications, and tools. We can access many of these tools by using the Solutions and Tools menus.
Create a libraryCreate a library
You can create SAS libraries using a point-and-click interface. Click View Explorer. Click File New. In the New Library window, specify information for the new library. If you
want the library to be created at the beginning of each SAS session, click Enable at startup.
Click OK.
Contd…Contd…
Assigning a libref to reference SAS files Assigning a libref to reference SAS files /*************************************/ /* define SAS library */ /*************************************/ libname sales 'c:\Test'; /*************************************//* create new data set from raw data *//*************************************/ data sales.quarter1; length Department $ 7 Site $ 8; input Department Site Quarter Sales; datalines; Parts Sydney 1 4043.97 Parts Atlanta 1 6225.26 Parts Paris 1 3543.97 Repairs Sydney 1 5592.82 Repairs Atlanta 1 9210.21 Repairs Paris 1 8591.98 Tools Sydney 1 1775.74 Tools Atlanta 1 2424.19 Tools Paris 1 5914.25 ; run; /*************************************/ /* print new data set *//*************************************/ proc print data=sales.quarter1; run;
EditorEditor
Log Log
OutputOutput
LibraryLibrary
Sales
Library & Data SetsLibrary & Data Sets
Assigning a libref to reference DBMS data Assigning a libref to reference DBMS data /*************************************/ /* define SAS library for Oracle */ /*************************************/ libname myorlib oracle user=scott password=tiger path="blunzer:v7" schema=hrdept;/*************************************/ /* define SAS library for DB2 */ /*************************************/ libname mydblib db2 noprompt="user=testuser; password=testpass;database=testdb"; /*************************************/ /* print Oracle table */ /*************************************/ proc print data=myorlib.all_employees; where state='CA'; run; /*************************************/ /* print DB2 table */ /*************************************/ proc print data=mydblib.customers; where state='CA'; run; /*************************************/ /* clear librefs */ /*************************************/ libname myorlib clear; libname mydblib clear;
Read PC database filesRead PC database files
If you have PC database files such as Microsoft Excel spreadsheets, Lotus spreadsheets, or Microsoft Access files, you can use SAS to import these files and create SAS data sets. Once you have the data in SAS data sets, you can process them as needed in SAS. To read PC database files, you use the IMPORT procedure. PROC IMPORT reads the input file and writes the data to a
SAS data set, with the SAS variables defined based on the input records.
ExampleExample
/*************************************//* import the Excel file */ /*************************************/ proc import datafile="c:\test\Accounts.xls" out=sasuser.accounts sheet="Prices"; getnames=no; run; /*************************************/ /* print part of the new data set */ /*************************************/ proc print data=sasuser.accounts(obs=10); run; /*************************************/ /* import the Access file */ /*************************************/ proc import table="customers" out=sasuser.cust dbms=access; uid="userid"; pwd="mypassword"; database="c:\test\east.mdb"; wgdb="c:\winnt\system32\security.mdb"; run; /*************************************/ /* print part of the new data set */ /*************************************/ proc print data=sasuser.cust(obs=5); run;
Read SAS data sets Read SAS data sets
DATA steps that read existing data sets using a SET statement. combine two or more input data sets to create one output
data set merge data from two or more input data sets that share a
common variable update a master file based on transaction records.
Work with SAS data setsWork with SAS data sets
Before we can work with our data in SAS, it must be in a special form called a SASdata set. Conceptually, a SAS data set (also called a table) is a file containing descriptor information and related data values. The file is organized as a table of observations (rows) and variables (columns) that SAS can process. Some SAS data sets also contain an index, which enables SAS to locate records in thedata set.
Contd…Contd…
To work with SAS data sets, you also need to understand how they are stored. All SAS files are stored in a SAS library, which is a collection of files such as SAS data sets and catalogs.In the Windows and Unix environments, a SAS library is typically a group of SAS files in thesame folder or directory.
In some operating environments, a SAS library is a physical collection of files. In others, thefiles are only logically related.
Questions