Sie sind auf Seite 1von 16

Introduction

To program effectively using SAS, you need to understand basic concepts about SAS programs and the SAS files that they process. In particular, you need to be familiar with SAS data sets. In this lesson, you'll examine a simple SAS program and see how it works. You'll learn details about SAS data sets (which are files that contain data that is logically arranged in a form that SAS can understand). Finally, you'll see how SAS data sets are stored temporarily or permanently in SAS libraries.

Editor
This window is a text editor. You can use it to type in, edit, and submit SAS programs as well as edit other text files such as raw data files. In Windows operating environments, the default editor is the Enhanced Editor. The Enhanced Editor is syntax sensitive and color codes your programs making it easier to read and find mistakes. The Enhanced Editor also allows you to collapse and expand the various steps in your program. For other operating environments, the default editor is the Program Editor whose features vary with the version of SAS and operating environment.

Log
The Log window contains notes about your SAS session, and after you submit a SAS program, any notes, errors, or warnings associated with your program as well as the program statements themselves will appear in the Log window.

Output
If your program generates any printable results, then they will appear in the Output window.

Results
The Results window is like a table of contents for your Output window; the results tree lists each part of your results in an outline form.

Explorer
The Explorer window gives you easy access to your SAS files and libraries.

SAS Programs Layout of SAS programs


There really arent any rules about how to format your SAS program. While it is helpful to have a neat looking program with each statement on a line by itself and indentions to show the various parts of the program, it isnt necessary. 1. 2. 3. 4. SAS statements can be in upper- or lowercase. Statements can continue on the next line (as long as you dont split words in two). Statements can be on the same line as other statements. Statements can start in any column.

The Two Parts of a SAS Program

DATA steps 1. Begin with DATA statements 2. Read and modify data 3. Create a SAS data set

PROC steps 1. Begin with PROC statements 2. Perform specific analysis or function 3. Produce results or report

Sample program Data work.clinic; Input Name$ Age Gender$; Cards; Shilpa 23 female Ravi 24 male Pradip 24 male Sweta 23 female Run; Characteristics of SAS Programs
Next let's look at the individual statements in our sample program. SAS programs consist of SAS statements. A SAS statement has two important characteristics:

It usually begins with a SAS keyword. It always ends with a semicolon.

Sample Program Code a DATA statement DATA work.clinic; a INPUT statement Input Name$ Age Gender$; a CARDS statement Cards; a RUN Statement Run;

Statements

Issue the SUBMIT command or click on Submit or select Run _ Submit to submit the program for execution.

Data types
Raw data come in many different forms, but SAS simplifies this. In SAS there are just two data types: numeric and character. Numeric fields are, well, numbers. They can be added and subtracted, can have any number of decimal places, and can be positive or negative. In addition to numerals, numeric fields can contain plus signs (+), minus signs (-), decimal points (.), or E for scientific notation. Character data are everything else. They may contain numerals, letters, or special characters (such as $ or !) and can be up to 32,767 characters long.

Rules for SAS names


You make up names for the variables in your data and for the data sets themselves. It is helpful to make up names that identify what the data represent, especially for variables. While the variable names A, B, and C might seem like perfectly fine, easy-to-type names when you write your program, the names Sex, Height, and Weight will probably be more helpful when you go back to look at the program six months later. Follow these simple rules when making up names for variables and data set members: 1. Names must be 32 characters or fewer in length.3 2. Names must start with a letter or an underscore (_). 3. Names can contain only letters, numerals, or underscores (_). No %$!*&#@, please.4 4. Names can contain upper- and lowercase letters.

Reading the SAS Log


Every time you run a SAS job, SAS writes messages in your log. Many SAS programmers ignore the SAS log and go straight to the output. Thats understandable, but dangerous. It is possible and sooner or later it happens to all of us to get bogus results that look fine in the output. The only way to know they are bad is to check the SAS log. Just because it runs doesnt mean its right.
1 2 3 DATA work.clinic; Input Name$ Age Gender$; Cards;

NOTE: The data set WORK.CLINIC has 4 observations and 3 variables. NOTE: DATA statement used (Total process time): real time 0.31 seconds cpu time 0.00 seconds 8 Run;

SAS Data Sets Overview of SAS Data Sets


As you saw in our sample program, for many of the data processing tasks that you perform with SAS, you

Access data in the form of a SAS data set Analyze, manage, or present the data.

Conceptually, a SAS data set is a file that consists of two parts: a descriptor portion and a data portion. Sometimes a SAS data set also points to one or more indexes, which enable SAS to locate records in the data set more efficiently. (The data sets that you work with in this lesson do not contain indexes.)

Length
A variable's length (the number of bytes used to store it) is related to its type.

Character variables can be up to 32K long. All numeric variables have a default length of 8. Numeric values (no matter how many digits they contain) are stored as floating-point numbers in 8 bytes of storage, unless you specify a different length.

Missing data
Sometimes despite your best efforts, your data may be incomplete. The value of a particular variable may be missing for some observations. In those cases, missing character data are represented by blanks, and missing numeric data are represented by a single period (.).

Size of SAS data sets


Prior to SAS 9.1, SAS data sets could contain up to 32,767 variables. Beginning with SAS 9.1, the maximum number of variables in a SAS data set is limited by the resources available on your computer but SAS data sets with more than 32,767 variables cannot be used with earlier versions of SAS. The number of observations, no matter which version of SAS you are using, is limited only by your computers capacity to handle and store them.

SAS Libraries

Sashelp
A permanent library, that contains sample data and other files which control how SAS works at your site. This is a read-only library.

Sasuser

A permanent library that contains SAS files in the Profile catalogs that store your personal settings. This is also a convenient place to store your own files.

Work
A temporary library for files that do not need to be saved from session to session.

Creating a new library


You can create new SAS libraries using the New Library window. To open this window, either left click in the Active Libraries window (to make it active) and choose New from the File menu, or right click in the Active Libraries window and choose New from the pop-up menu.

In the New Library window, type the name of the library you want to create. This name is called a libref which is short for library reference. A libref must be eight characters or fewer; start with a letter or underscore; and contain only letters, numerals, or underscores. In this window, the name Mylib has been typed in as the libref. In the Path field, enter the complete path to the folder or directory where you want your data sets to be stored, or choose the Browse button to navigate to the location. If you dont want to define your library reference every time you start up SAS, then check the Enable at startup box. Click OK and then your new library reference will appear in the Active Libraries window. Here is the Active Libraries window showing the newly created Mylib library.

OR Libname Mylib D: \;
- in Editor Window.

Deleting Data sets Proc datasets library = SAS library name; Delete SAS-dataset list; Run; Properties of Data sets

PROC CONTENTS DATA = SAS-file-specification NODS; RUN;

SAS-file-specification specifies an entire library or a specific SAS data set within a


library. SAS-file-specification can take one of the following forms:
o o

<libref.>SAS-data-set names one SAS data set to process. <libref.>_ALL_ requests a listing of all files in the library. (Use a
period (.) to append _ALL_ to the libref.)

NODS suppresses the printing of detailed information about each file when you
specify _ALL_. (You can specify NODS only when you specify _ALL_.)

Selected Menus (as displayed in the Windows operating environment) Select items from this menu ... To ...

open main SAS windows. From the Explorer window, you can use this menu to show or hide details and a tree view.

submit and recall SAS programming statements in the Program Editor window.

access ready-to-use solutions and applications.

get more help.

SAS System Options


If you create your procedure output as a SAS listing, you can also control the appearance of your output by setting system options such as

line size (the maximum width of the log and output) page size (the number of lines per printed page of output) The display of page numbers. The display of date and time.

Not all options are available for all operating environments. A list of options specific to your operating environment appears in the SAS Help and Documentation. You can see a list of system options and their current values by opening the SAS System Options window or by using the OPTIONS procedure. To use the OPTIONS procedure, submit the following SAS program and view the results in the SAS log:

PROC OPTIONS; RUN;

Common options
The following are some common system options you might want to use: CENTER | NOCENTER DATE | NODATE Controls whether output are centered or left-justified. Default: CENTER. Controls whether or not todays date will appear at the top of each page of output. Default: DATE. Controls the maximum length of output lines. Possible values for n are 64 to 256. Default varies. Controls whether or not page numbers appear on each page of SAS output. Default: NUMBER.

LINESIZE = n NUMBER | NONUMBER

ORIENTATION = PORTRAIT Specifies the orientation for printing output. ORIENTATION = LANDSCAPE Default: PORTRAIT PAGENO = n PAGESIZE = n Starts numbering output pages with n. Default is 1. Controls the maximum number of lines per page of output. Possible values for n are 15 to 32767. Default varies. Specifies size of margin (such as 0.75in or 2cm) to be printing output. Default: 0.00in.

RIGHTMARGIN = n LEFTMARGIN = n TOPMARGIN = n BOTTOMMARGIN = n

Extraction of Data Extraction from notepad


Create notepad file with the name of sales and put following data For extraction of data from notepad write program in Editor Window of SAS. Create file in notepad with following data with name of Sales Chocolate 213 123 Vanilla 213 512 Chocolate 415 242

Write following program in SAS Editor Window.

DATA icecream; INFILE c:\MyRawData\Sales.txt; INPUT Flavor $ 1-9 Location BoxesSold; RUN; Extraction from Excel
Using the Import Wizard1, you can read a variety of data file types into SAS by simply answering a few questions. The Import Wizard will scan your file to determine variable types2 and will, by default, use the first row of data for the variable names. Start the Import Wizard by choosing Import Data from the File menu. Select the type of file you are importing by choosing from the list of standard data sources such as Excel 97, excel 2000, etc...

OR Proc import file = path of excel file out = sas-data set name; Run;

SET copying datasets


The SET statement is flexible and has a variety of uses in SAS programming. These uses are determined by the options and statements that you use with the SET statement. They include

reading observations and variables from existing SAS data sets for further processing in the DATA step concatenating and interleaving data sets, and performing one-to-one reading of data sets Reading SAS data sets by using direct access methods.

Data test; Set Sashelp.Air; Run;


FIRSTOBS= The FIRSTOBS= option tells SAS at what line to begin reading data.

Data test; Set Sashelp.air (firstobs = 10); Run;


OBS= The OBS= option can be used anytime you want to read only a part of your data file. It tells SAS to stop reading when it gets to that line in the raw data file.

Data test; Set Sashelp.air (obs = 10); Run;

Data test; Set sashelp.class(firstobs =5 obs = 10); Run;


In above example, work.test having 6 observations.

Das könnte Ihnen auch gefallen