IntroductionTo program effectively using SAS, you need to understand basic concepts about SASprograms and the SAS files that they process. In particular, you need to be familiarwith SAS data sets.In this lesson, youll examine a simple SAS program and see how it works. Youll learndetails about SAS data sets (which are files that contain data that is logically arrangedin a form that SAS can understand). Finally, youll see how SAS data sets are storedtemporarily or permanently in SAS libraries.EditorThis window is a text editor. You can use it to type in, edit, and submit SAS programs aswell 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 andcolor codes your programs making it easier to read and find mistakes. The EnhancedEditor also allows you to collapse and expand the various steps in your program. Forother operating environments, the default editor is the Program Editor whose featuresvary with the version of SAS and operating environment.
LogThe Log window contains notes about your SAS session, and after you submit a SASprogram, any notes, errors, or warnings associated with your program as well as theprogram statements themselves will appear in the Log window.OutputIf your program generates any printable results, then they will appear in the Outputwindow.ResultsThe Results window is like a table of contents for your Output window; the results treelists each part of your results in an outline form.ExplorerThe Explorer window gives you easy access to your SAS files and libraries. SAS Programs Layout of SAS programsThere really aren’t 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 byitself and indentions to show the various parts of the program, it isn’t necessary.1. SAS statements can be in upper- or lowercase.2. Statements can continue on the next line (as long as you don’t split words in two).3. Statements can be on the same line as other statements.4. Statements can start in any column. The Two Parts of a SAS Program
DATA steps PROC steps1. Begin with DATA statements 1. Begin with PROC statements2. Read and modify data 2. Perform specific analysis or function3. Create a SAS data set 3. Produce results or reportSample programData work.clinic;Input Name$ Age Gender$;Cards;Shilpa 23 femaleRavi 24 malePradip 24 maleSweta 23 femaleRun;Characteristics of SAS ProgramsNext lets look at the individual statements in our sample program. SAS programs consistof SAS statements. A SAS statement has two important characteristics: • It usually begins with a SAS keyword. • It always ends with a semicolon. Statements Sample Program Code a DATA statement DATA work.clinic; a INPUT statement Input Name$ Age Gender$; a CARDS statement Cards; a RUN Statement Run;Issue the SUBMIT command or click on Submit or select Run _ Submit to submit theprogram for execution.
Data typesRaw data come in many different forms, but SAS simplifies this. In SAS there are justtwo data types: numeric and character. Numeric fields are, well, numbers. They can beadded and subtracted, can have any number of decimal places, and can be positive ornegative. 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 to32,767 characters long.Rules for SAS namesYou make up names for the variables in your data and for the data sets themselves. It ishelpful 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 nameswhen you write your program, the names Sex, Height, and Weight will probably be morehelpful when you go back to look at the program six months later. Follow these simplerules 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 LogEvery time you run a SAS job, SAS writes messages in your log. Many SASprogrammers ignore the SAS log and go straight to the output. That’s understandable, butdangerous. It is possible and sooner or later it happens to all of us to get bogus results thatlook fine in the output. The only way to know they are bad is to check the SAS log. Justbecause it runs doesn’t mean its right. 1 DATA work.clinic; 2 Input Name$ Age Gender$; 3 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 SetsAs you saw in our sample program, for many of the data processing tasks that youperform 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 anda data portion. Sometimes a SAS data set also points to one or more indexes, whichenable SAS to locate records in the data set more efficiently. (The data sets that you workwith in this lesson do not contain indexes.)LengthA variables 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 dataSometimes despite your best efforts, your data may be incomplete. The value of aparticular variable may be missing for some observations. In those cases, missingcharacter data are represented by blanks, and missing numeric data are represented by asingle period (.).Size of SAS data setsPrior 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 limitedby the resources available on your computer but SAS data sets with more than 32,767variables cannot be used with earlier versions of SAS. The number of observations, nomatter which version of SAS you are using, is limited only by your computer’s capacityto handle and store them.SAS LibrariesSashelpA permanent library, that contains sample data and other files which control how SASworks at your site. This is a read-only library.Sasuser
A permanent library that contains SAS files in the Profile catalogs that store yourpersonal settings. This is also a convenient place to store your own files.WorkA temporary library for files that do not need to be saved from session to session.Creating a new libraryYou 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 fromthe File menu, or right click in the Active Libraries window and choose New from thepop-up menu.
In the New Library window, type the name of the library you want to create. This name iscalled a libref which is short for library reference. A libref must be eight characters orfewer; 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 thecomplete path to the folder or directory where you want your data sets to be stored, orchoose the Browse… button to navigate to the location. If you don’t want to define yourlibrary reference every time you start up SAS, then check the Enable at startup box. ClickOK and then your new library reference will appear in the Active Libraries window. Hereis the Active Libraries window showing the newly created Mylib library. ORLibname 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 alibrary. SAS-file-specification can take one of the following forms: o <libref.>SAS-data-set names one SAS data set to process. o <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 canuse this menu to show or hide detailsand a tree view.submit and recall SAS programmingstatements in the Program Editorwindow.access ready-to-use solutions andapplications.
get more help.SAS System OptionsIf you create your procedure output as a SAS listing, you can also control the appearanceof 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 toyour operating environment appears in the SAS Help and Documentation. You can see alist of system options and their current values by opening the SAS System Optionswindow or by using the OPTIONS procedure. To use the OPTIONS procedure, submitthe following SAS program and view the results in the SAS log:PROC OPTIONS;RUN;
Common optionsThe following are some common system options you might want to use:CENTER | NOCENTER Controls whether output are centered or left-justified. Default: CENTER.DATE | NODATE Controls whether or not today’s date will appear at the top of each page of output. Default: DATE.LINESIZE = n Controls the maximum length of output lines. Possible values for n are 64 to 256. Default varies.NUMBER | NONUMBER Controls whether or not page numbers appear on each page of SAS output. Default: NUMBER.ORIENTATION = PORTRAIT Specifies the orientation for printing output.ORIENTATION = LANDSCAPE Default: PORTRAITPAGENO = n Starts numbering output pages with n. Default is 1.PAGESIZE = n Controls the maximum number of lines per page of output. Possible values for n are 15 to 32767. Default varies.RIGHTMARGIN = n Specifies size of margin (such as 0.75in or 2cm) to beLEFTMARGIN = n printing output. Default: 0.00in.TOPMARGIN = nBOTTOMMARGIN = n
Extraction of DataExtraction from notepadCreate 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 ofSalesChocolate 213 123Vanilla 213 512Chocolate 415 242Write following program in SAS Editor Window.DATA icecream;INFILE ’c:MyRawDataSales.txt’;INPUT Flavor $ 1-9 Location BoxesSold;RUN;Extraction from ExcelUsing the Import Wizard1, you can read a variety of data file types into SAS by simplyanswering a few questions. The Import Wizard will scan your file to determine variabletypes2 and will, by default, use the first row of data for the variable names. Start theImport Wizard by choosing Import Data… from the File menu. Select the type of file youare importing by choosing from the list of standard data sources such as Excel 97, excel2000, etc... ORProc import file = ‘path of excel file’ out = sas-data set name;Run;
SET – copying datasetsThe SET statement is flexible and has a variety of uses in SAS programming. These usesare determined by the options and statements that you use with the SET statement. Theyinclude • 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 datafile. 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.