1. Base Programming GIORGIO RUSSOLILLO Cours de prépara+on à la cer+fica+on SAS «Base Programming» 9
What is SAS Highly flexible and integrated soiware environment; you can use SAS for: GIORGIO RUSSOLILLO Cours de prépara+on à la cer+fica+on SAS «Base Programming» 10
Base SAS The founda+on of SAS soiware is called Base SAS. GIORGIO RUSSOLILLO Cours de prépara+on à la cer+fica+on SAS «Base Programming» 11
This course assumes that you are running Base SAS soiware in the windowing environment Which programming workspace am I using? Help > About GIORGIO RUSSOLILLO Cours de prépara+on à la cer+fica+on SAS «Base Programming» 12
Set up prac+ce data To create a permanent SAS library named sasuser : Help > Learning SAS programming > OK GIORGIO RUSSOLILLO Cours de prépara+on à la cer+fica+on SAS «Base Programming» 13
SAS program A SAS program is a data file that is formaaed in a way that SAS can understand. It can consist of one or more steps: a DATA step, a PROC step or any combina+on of the two GIORGIO RUSSOLILLO Cours de prépara+on à la cer+fica+on SAS «Base Programming» 14
Data step With a Data step you can create or modify data sets: You use a Data step for: Put your data in a SAS dataset Compute values Check for and correct errors in your data Produce new SAS datasets by subsepng, supersepng, merging and upda+ng exis+ng data GIORGIO RUSSOLILLO Cours de prépara+on à la cer+fica+on SAS «Base Programming» 15
Proc Step Invoke prewriaen rou+nes that enable you to analyze and process the data in a SAS dataset You can use PROC setps to: Create a report that lists the data Produce decrip+ve sta+s+cs Create a summary report Produce plot and chart GIORGIO RUSSOLILLO Cours de prépara+on à la cer+fica+on SAS «Base Programming» 16
Generally a program involves a combina+on of the two steps like this: GIORGIO RUSSOLILLO Cours de prépara+on à la cer+fica+on SAS «Base Programming» 17
SAS Statements A SAS program consists of statements A SAS statement: Usually begins with a SAS keyword Always ends with a semicolon GIORGIO RUSSOLILLO Cours de prépara+on à la cer+fica+on SAS «Base Programming» 18
A SAS program GIORGIO RUSSOLILLO Cours de prépara+on à la cer+fica+on SAS «Base Programming» 19
Understanding a SAS program Tree steps.. GIORGIO RUSSOLILLO Cours de prépara+on à la cer+fica+on SAS «Base Programming» 20
.. 13 statements.. GIORGIO RUSSOLILLO Cours de prépara+on à la cer+fica+on SAS «Base Programming» 21
.. Among them two global statements : Global statements are allalonestatements. You can use global statements anywhere in a SAS program. Global statements are not executable; they take effect as soon as SAS compiles program statements. GIORGIO RUSSOLILLO Cours de prépara+on à la cer+fica+on SAS «Base Programming» 22
SAS statements are free format They can begin and end anywhere on a line A statement can con+nue on several lines Several statements can be on the same line Blanks or special characters separare words in a SAS statement GIORGIO RUSSOLILLO Cours de prépara+on à la cer+fica+on SAS «Base Programming» 23
Processing a program DATA sasuser.admit2; SET sasuser.admit; WHERE age>39; RUN; Bounderies of a step: PROC PRINT DATA=sasuser.admit2; A step begins by DATA or PROC statements RUN; A step ends by RUN or QUIT statement (although the RUN statement is not always required between steps, using it can make the SAS program easier to read and debug) At the step boundary, SAS: i) executes any statements that have not previously executed ii) ends the step Steps are submiaed in Enhanced Editor or in Program Editor windows When you submit a program, SAS execute it step by step. A log is generated in the Log window (for both PROC and DATA steps) Reports are generated in Output (as a list) and in Results Viewer (in HTML) windows for most of PROC steps (excep+ons: copy, sort...). You can find reports also in Results window. GIORGIO RUSSOLILLO Cours de prépara+on à la cer+fica+on SAS «Base Programming» 24
Report Window Some SAS program open an interac+ve window, such as a REPORT window. You can use this window to directly modify data PROC REPORT DATA=sasuser.admit WINDOWS; COLUMNS id nams sex age actlevel; RUN; GIORGIO RUSSOLILLO Cours de prépara+on à la cer+fica+on SAS «Base Programming» 25
Programming workspace: Editor Windows Enhanced Editor Features: Program Editor Features: Opening SAS programs Entering, edi+ng and submipng SAS programs Using the command line or menus Saving SAS programs Clearing contents Opening SAS programs Entering, edi+ng and submipng SAS programs Using the command line or menus Saving SAS programs Clearing contents Color Coding and sintax checking Expandable and collapsibile sec+ons Recordable macros Support for keyboard shortcuts Mul+level undo and redo Recalling Submiaed statements For clear any of these windows, when the window is ac+ve: Edit > Clear all GIORGIO RUSSOLILLO Cours de prépara+on à la cer+fica+on SAS «Base Programming» 26
Programming workspace: Editor Windows GIORGIO RUSSOLILLO Cours de prépara+on à la cer+fica+on SAS «Base Programming» 27
Sepng result formats Two formats: Lis+ng and HTML. Different versions, different default. Tools > Op+ons > Preferences > Results Both HTML and LISTING are the default in SAS 9.3 in Windows and UNIX Just HTML is the default in SAS 9.4 in Windows and UNIX In other plavorms, you have just LISTING mode, but you can create HTML output using programming statements You can choose to show the HTML output in another browser GIORGIO RUSSOLILLO Cours de prépara+on à la cer+fica+on SAS «Base Programming» 28
The Log windows The Log window displays messages about your SAS session and about any SAS programs that you submit. To display this window : View > Log To clear this window : Edit > Clear all GIORGIO RUSSOLILLO Cours de prépara+on à la cer+fica+on SAS «Base Programming» 29
The output windows The Output and Result Viewer windows list output from SAS program that you submit. Output shows results in LISTING mode, Results Viewer shows results in HTML mode By default they are behind the editor and the log windows. When you create output, they automa+cally move to the front of your display (Output behind Result Viewer). To display output window : View > Output The Results window lists and arranges in a tree structure the output. It allows browsing output easily It allows viewing, saving and prin+ng individual items of output It dislays separate icons for LISTING and HTML outputs To display this window : View > Results GIORGIO RUSSOLILLO Cours de prépara+on à la cer+fica+on SAS «Base Programming» 30
The explorer window The Explorer window allows you to view and manage SAS files (in the libraries) but also nonsas (external) files. create new libraries and SAS files Moving, coping and dele+ng files Create shortcuts to external files. To display this window : View > Explorer GIORGIO RUSSOLILLO Cours de prépara+on à la cer+fica+on SAS «Base Programming» 31
SAS Libraries A SAS library is a collec+on of SAS files stored in the same directory Temporary SAS libraries last only for the current session Permanent SAS Libraries are available during subsequent sessions GIORGIO RUSSOLILLO Cours de prépara+on à la cer+fica+on SAS «Base Programming» 32
Referring a SAS dataset Two level (divided by a period) names: libref dataset name They refer to the same (temporary) library GIORGIO RUSSOLILLO Cours de prépara+on à la cer+fica+on SAS «Base Programming» 33
If you don t specify a library name when you create a file (or you specify the library name Work), the file is stored in the temporary SAS data library GIORGIO RUSSOLILLO Cours de prépara+on à la cer+fica+on SAS «Base Programming» 34
Rules for SAS filenames and libref Filename Can be 1 to 32 characters long Must begin with a leaer (uppercase or lowercase) or an underscore Can con+nue with any combina+on of numbers, leaers or underscores Ex: Payroll, LABDATA1995_1997, E+matedTaxPayments3 Libref Can be 1 to 8 characters long Must begin with a leaer (uppercase or lowercase) or an underscore Can con+nue with any combina+on of numbers, leaers or underscores GIORGIO RUSSOLILLO Cours de prépara+on à la cer+fica+on SAS «Base Programming» 35
SAS Datasets A SAS Dataset is a file that consists of two parts: a descriptor por,on and a data por,on GIORGIO RUSSOLILLO Cours de prépara+on à la cer+fica+on SAS «Base Programming» 36
Descrip+on por+on PROC CONTENTS DATA=sasuser.insure; RUN; Name of the dataset Date and +me the dataset was created The number of observa+ons The number of variables The properies (akributes) of each variable GIORGIO RUSSOLILLO Cours de prépara+on à la cer+fica+on SAS «Base Programming» 37
Variable Aaributes: Name Variable Name, as the File Name: Can be 1 to 32 characters long Must begin with a leaer (uppercase or lowercase) or an underscore Can con+nue with any combina+on of numbers, leaers or underscores GIORGIO RUSSOLILLO Cours de prépara+on à la cer+fica+on SAS «Base Programming» 38
Variable Aaributes: Type Can contain any values A blank represents a missing value for character variables Can contain only numeric values A period represents a missing value for numeric variables GIORGIO RUSSOLILLO Cours de prépara+on à la cer+fica+on SAS «Base Programming» 39
Variable Aaributes: Length Length is measured in bytes (1 character > 1 byte) Numeric variables are stored by default in 8 bytes GIORGIO RUSSOLILLO Cours de prépara+on à la cer+fica+on SAS «Base Programming» 40
Variable aaributes: Format & Informat Format indicates how a data value is displayed by SAS Ex: To display the value 1234 as $1,234.00 you can use the DOLLAR8.2 format Informat indicates how a raw data value is read by SAS in order to be converted in a SAS data value Ex: $12,345.00 contains two special characters. You can use informat op+on COMMA10. for removing dollar symbol and comma, in order to store 12345 as a numeric value GIORGIO RUSSOLILLO Cours de prépara+on à la cer+fica+on SAS «Base Programming» 41
Variable aaributes: Label The label is a descrip+ve text up to 256 character long GIORGIO RUSSOLILLO Cours de prépara+on à la cer+fica+on SAS «Base Programming» 42
Data por+on It is a collec+on of values arranged in a rectangular table PROC PRINT DATA=sasuser.insure noobs; RUN; GIORGIO RUSSOLILLO Cours de prépara+on à la cer+fica+on SAS «Base Programming» 43
File Shortcuts You can create a file shortcut to an external file and store it in the File Shortcuts folder in the Explorer window. fileref is an op+onal name that is used to iden+fy an external file in SAS. you can se a file shortcut to open browse and submit a file When you delete a file shortcut, the pointer to the file is deleted. However the file s+ll exists in its phisical loca+on GIORGIO RUSSOLILLO Cours de prépara+on à la cer+fica+on SAS «Base Programming» 44
Help Using this window is a task oriented help referring to the ac+ve window. Other op+ons of the help menu: Sas help and Documenta+on (complete guide to SAS) Gepng started tutorials Learning SAS programming (create dataset used in training) SAS on the web (technical support) GIORGIO RUSSOLILLO Cours de prépara+on à la cer+fica+on SAS «Base Programming» 45