ch 1. introduction to base sas

24
Chapter 1 Introdution to Base Sas

Upload: amcuc

Post on 08-Sep-2015

241 views

Category:

Documents


3 download

DESCRIPTION

dsf

TRANSCRIPT

Introduction to SAS Base

Chapter 1Introdution to Base Sas1The GoalSAS Base is a very versatile and flexible programming environment. Our goal is to learn SAS at a level that we can conduct Survival Analysis.Also, to provide the students with a crash course for the basic understanding of SAS Base environment2What is Base SAS?It is a software suit for advanced analytics, business intelligence, data management and predictive analytics.

SAS is short for Statistical Analysis System. The company dropped the longer name once the abbreviation, SAS, became more common than the original one.

When someone is talking about SAS, she may be referencingSAS InstituteSAS softwareSAS language

3SAS LanguageSection 14SAS programsMany software applications are either menu driven, or command driven (enter a command - see the result)SAS is a statement-driven language. You use a list of statements as instructions to write SAS programs.

Definition: A SAS Program is a sequence of statements executed in order to accomplish a certain task.

SAS Enterprise Guide or SAS JMP are point-and-click environments for writing SAS programs but they don't offer the flexibility of Base SAS.5An Everyday Analogy to SAS ProgramsI would like to make a withdrawal My account number is 1234I would like $200.Give me five 20s and two 50s.

I would like to make a withdrawal I would like $200.Give me five 20s and two 50s.My account number is 1234

You first say what you want to do, then give all the information teller needs.The order of the details may change, but what you want to do must be always the first. 6A SAS Program Example* Read animals weights from file; -SAS CommentDATA animals;INFILE c:\MyRawData\Zoo.dat; -SAS StatementINPUT Lions Tigers; -SAS StatementPROC PRINT DATA = animals; -SAS Statement /* Print the results */ -SAS CommentRUN; -SAS Statement

Very important:Every SAS statement ends with a semicolon (;).

7SAS Programming RulesFirst rule is there arent many syntax rules. The only real rule is every statement ends with a semicolon.The syntax and format are very flexible.It is not case sensitive.Statements can continue on the next line. Also, there can be more than one statement on one line.Statements can start on any column of the text editor.Since SAS is this flexible, it is easy to write SAS code that cannot be recognized even by the coder. We need to be tidy and organized:Use indentionUse naming conventionUse casing convention (part of naming convention)

8Some Naming ExamplesUse the same rules in the same code, even in your career if possible.numPrice, numPrice_Toys, numPrice_Hotel_StaycharName, charName_City, charName_Baseball_Team name, nameCity, nameBaseballTeamcalories_sugar, fiber_sugar, protein_sugar

a = b * c, instead write discountAmount = priceItem * discountPercentage

(If m=civic then b=Honda), instead writeIf model_bought = civic then make_bought = Honda

SAS Data SetsSection 210Variables and ObservationsData point (or cell) Calvin shows the value for the 3rd observation and second variable Name.IDNameHeightWeight53Susie424154Charlie465555Calvin403556Lucy465257Dennis44.584350Observation(also called rows)Variable (also called columns)

11Data TypesTwo data types: NumericCharacterNull value for character is shown with a blank cell Null value for numeric is shown with a dot.IDNameHeightWeight53Susie424154Charlie465555Calvin403556Lucy465257Dennis44.584350

Rules for SAS NamesNames must be 32 characters or fewer.Names must start with a letter or an underscore (_) .Names can contain upper- and lowercase letters.Names can contain only letters, numbers or underscore. No %$!*@, please.

Using naming conventions can make your life easier if a need for debugging or returning back to your code arises.

Two Parts of a SAS ProgramSection 3An ExampleDATA Step

PROC StepDATA distance;Miles = 26.22;Kilometers = 1.61 * Miles;RUN;

PROC PRINT DATA = distance;RUN;

Output of the Example

Common Properties of SAS ProgramsThey are made up of statements. Some statements work in DATA and not PROC step or vice versa.Remember this to avoid falling into a common pitfall of using the wrong statement for the wrong step.DATA steps read, modify and combine dataPROC steps analyze data, perform utility functions, or print reports. A step ends when:There is a new step keyword (i.e. DATA or PROC)There is a RUN statementDifferent CharacteristicsDATA StepsPROC Stepsbegin with DATA statementsbegin with PROC statementsRead, modify and combine dataperform specific analysis or functionsceate a SAS data setproduce results or reportcan include IF-THEN, DO statementsNot very common to include IF-THEN, DO statementsReminder: Keep in mind that, the above table is only a simplification. Because SAS is so flexible, the differences between DATA and PROC steps are more blurry.DATA Steps Built-in LoopDATA steps execute line by line and then observation by observation.

You dont need to tell SAS to execute this loop. It is handled automatically as opposed to many coding languages. It is like T-SQL in that sense more than C#.

SAS Data LibrariesSection 4LibrariesBefore you can use a SAS data set, you have to tell SAS where to find it by setting up a SAS library.A SAS library is simply a location where SAS data sets are stored. It is like an address or a reference for a physical storage unit.Ways to set up a library:Using LIBNAME statementUsing New Library windowYou can navigate libraries using the Explorer window.Active Libraries WindowLibraries on Start-upSASHELPSASUSERWORK: It is the default libraryIt is the temporary storage location for SAS datasets.All data in this library is deleted once you end you SAS session.Creating a New LibraryBy New Library windowTo open it: (In Base SAS but not in SAS University)Left click on the Active Libraries window (to make it active), 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.To the Path field, enter the complete path or directory of where you want to store the data sets. By libname statement:LIBNAME ;Can be tricky sometimes.The name can be at most 8 characters and can contain letters, numerals or underscore, it cant start with numerals.

Other FunctionsViewing data sets in the viewtable windowContents windowViewtable windowChanging Column headingsColumn optionsViewing the properties of data sets with SAS ExplorerOpening the Properties windowGeneral tabColumns tab