Summer SAS Workshop Lecture 3. Summer 20072 SAS Workshop Website www.musc.edu/~simpsona/SASWorkshop/

Slides:



Advertisements
Similar presentations
Escape Sequences \n newline \t tab \b backspace \r carriage return
Advertisements

Debugging ACL Scripts.
Chapter 3: Editing and Debugging SAS Programs. Some useful tips of using Program Editor Add line number: In the Command Box, type num, enter. Save SAS.
CS0007: Introduction to Computer Programming
Programming in Visual Basic
I OWA S TATE U NIVERSITY Department of Animal Science Modifying and Combing SAS Data Sets (Chapter in the 6 Little SAS Book) Animal Science 500 Lecture.
SAS Programming: Working With Variables. Data Step Manipulations New variables should be created during a Data step Existing variables should be manipulated.
I OWA S TATE U NIVERSITY Department of Animal Science Getting Started Using SAS Software Animal Science 500 Lecture No. 2.
Objectives In this chapter, you will learn about:
Debugging Introduction to Computing Science and Programming I.
Mean Comparison With More Than Two Groups
Introduction to a Programming Environment
1 Computer Applications in Epidemiology Dongmei Li Lecture 26 5/6/2009.
Pet Fish and High Cholesterol in the WHI OS: An Analysis Example Joe Larson 5 / 6 / 09.
Chapter 18: Modifying SAS Data Sets and Tracking Changes 1 STAT 541 ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
Chapter Seven Advanced Shell Programming. 2 Lesson A Developing a Fully Featured Program.
CHAPTER 4: INTRODUCTION TO COMPUTER ORGANIZATION AND PROGRAMMING DESIGN Lec. Ghader Kurdi.
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
SAS Workshop Lecture 1 Lecturer: Annie N. Simpson, MSc.
1 Operating Systems Lecture 3 Shell Scripts. 2 Shell Programming 1.Shell scripts must be marked as executable: chmod a+x myScript 2. Use # to start a.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
Introduction to SAS BIO 226 – Spring Outline Windows and common rules Getting the data –The PRINT and CONTENT Procedures Manipulating the data.
General Programming Introduction to Computing Science and Programming I.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
HOMEWORK REVIEW This is an if else statement layout if (condition) { code to be executed if condition is true; } else { code to be executed if condition.
CHAPTER 4: CONDITIONAL STRUCTURES Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
Quantify the Example Data First, code and quantify the data (assign column locations & variable names) Use the sample data to create a data set from the.
SAS Macro: Some Tips for Debugging Stat St. Paul’s Hospital April 2, 2007.
BMTRY 789 Introduction to SAS Programming Lecturer: Annie N. Simpson, MSc.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
CSE1222: Lecture 6The Ohio State University1. Common Mistakes with Conditions (1)  Consider the following code: int age(26); if (age = 18) { cout
CPS120: Introduction to Computer Science Decision Making in Programs.
Review, Pseudocode, Flow Charting, and Storyboarding.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
22/11/ Selection If selection construct.
BMTRY 789 Lecture 11: Debugging Readings – Chapter 10 (3 rd Ed) from “The Little SAS Book” Lab Problems – None Homework Due – None Final Project Presentations.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
 2000 Deitel & Associates, Inc. All rights reserved. Outline 8.1Introduction 8.2A Simple Program: Printing a Line of Text in a Web Page 8.3Another JavaScript.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
Computing with SAS Software A SAS program consists of SAS statements. 1. The DATA step consists of SAS statements that define your data and create a SAS.
CS116 COMPILER ERRORS George Koutsogiannakis 1. How to work with compiler Errors The Compiler provide error messages to help you debug your code. The.
Lecture 7 Conditional Scripting and Importing/Exporting.
Controlling Program Flow with Decision Structures.
IST 210: PHP LOGIC IST 210: Organization of Data IST210 1.
Sequential Processing to Update a File Please use speaker notes for additional information!
Chapter 6: Modifying and Combining Data Sets  The SET statement is a powerful statement in the DATA step DATA newdatasetname; SET olddatasetname;.. run;
1 EPIB 698C Lecture 1 Instructor: Raul Cruz-Cano
Based on Learning SAS by Example: A Programmer’s Guide Chapters 1 & 2
The Urban Institute - SAS Training6/9/20161 SAS Training This SAS Training Course was designed to introduce users at The Urban Institute to SAS programming.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
CS 115 Lecture 5 Math library; building a project Taken from notes by Dr. Neil Moore.
Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
Appendix A Barb Ericson Georgia Institute of Technology May 2006
CS0007: Introduction to Computer Programming
Chapter 6: Modifying and Combining Data Sets
Debugging and Random Numbers
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Conditions and Ifs BIS1523 – Lecture 8.
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
An Introduction to Debugging
CHAPTER 6 Testing and Debugging.
Workshop for Programming And Systems Management Teachers
How to Run a Java Program
Presentation transcript:

Summer SAS Workshop Lecture 3

Summer SAS Workshop Website

Part I of Lecture 3 Thinking through a programming problem Programming logic Subsetting data

Summer How to program? What are your goals? What does your data look like? How does your data need to look to accomplish your goals? What is the first thing you type and run when you open SAS and want to start coding?????

Summer Dropping or Keeping Variables You often get big data sets that you only want to use part of. First Drop the variables that you don’t want. (or keep the ones you do want) Data newdata; set annie.olddataset; Keep name ssnumber visdate dob; Run; Or Data newdata; set annie.olddataset (Keep= name ssnumber visdate dob); Run;

Summer Conditional Logic [If-Then-Else] Frequently, you want an assignment statement to apply to some observations but not all - under some conditions, but not others. This is also how you create new variables by recategorizing the old variables into new groupings. 1)IF condition THEN action; 2)IF condition THEN action; ELSE IF condition THEN action; 3)IF condition THEN action; ELSE IF condition THEN action; ELSE action;

Summer IF-THEN-ELSE Rules A single IF-THEN statement can only have one action. If you add the keywords DO and END, then you can execute more than one action (put it in a loop). You can also specify multiple conditions with the keywords AND and OR *Remember SAS considers missing values to be smaller than non-missing values.

Summer Comparison Operators These operators can be coded using Symbols or Mnemonics. SymbolMnemonicMeaning =EQEquals ~=NENot Equal >GTGreater Than <LTLess Than >=GEGreater than or Equal <=LELess than or Equal &ANDAll comparisons must be true |OROnly one comparison must be true

Summer Subsetting Often programmers find that they want to use some of the observations in a data set and exclude the rest. The most common way to do this is with a subsetting IF statement in a DATA step. Syntax: IF expression;Ex: IF Sex = ‘f’;

Summer Subsetting (cont.) If the expression is true, then SAS continues with the DATA step. If the expression is false, then no further statements are processed for that observation; that observation is not added to the data set being created; and SAS moves to the next observation. While the subsetting IF statement tells SAS which observations to include, the DELETE statement tells SAS which observations to exclude: IF expression THEN DELETE; IF Sex = ‘m’ THEN DELETE; (same as If Sex = “f”;)

Summer Open SAS code from website Go through the code. Run the program. Questions? How could we make a new data set with only Males in it?

Part II of Lecture 3 Merging data sets SAS Functions

Summer It’s all in the way that you look at things …

Summer Combining Data Sets Using One-to-One Match Merge When you have two data sets with related data and you want to combine them. If you merge two data sets, and they have variables with the same names – besides the BY variables, then variables from the second data set will overwrite any variables having the same names in the first data set. All observations from old data sets will be included in the new data set whether they have a match or not.

Summer Match Merge Example Proc Sort Data = Rat; BY RatID Date; Run; Proc Sort Data = Rat2; BY RatID Date; Run; DATA BigRat; MERGE Rat Rat2; BY RatID Date; Run;

Summer SAS Functions Previously created SAS functions are used to simplify some complex programming problems Usually arithmetic or mathematical calculations Syntax of Function used in an expression: NewVar = FunctionName (VariableName);

Summer Common Functions Log ( ); Log10 ( ); Sin ( ); Cos ( ); Tan ( ); Int ( ); SQRT ( ); Weekday ( ); MDY (,, ); Round (x, 1); Mean ( ); RANUNI ( ); Put ( ); Input ( ); Lag ( ); Dif ( ); N ( ); NMISS ( );

Part III of Lecture 3 Debugging

Summer Debugging? “If debugging is the process of removing bugs, then programming must be the process of putting them in.” –From some strange, but insightful website

Summer Syntactic Errors vs. Logic Errors We will focus mainly on syntax errors; however, it is also possible for SAS to calculate a new variable using syntactically correct code that results in inaccurate calculations, I.e. a logic error. For this reason, it is always wise to check values of a new variable against values of the original variable used in the calculation.

Summer READ THE LOG WINDOW!! I know that I spout this all of the time, and that is because too many people begin skipping this step and then can’t figure out why their program isn’t working If you have an ERROR message, look at that line as well as a few of the lines above it Don’t ignore Warnings and Notes in the log simply because your program seems to have run, they could indicate a serious error that just did not happen to be syntactically incorrect, in this case, check your logic or add some Proc Prints to understand what is going on inside your program

Summer Debugging: The Basics The better you can read and understand your program, the easier it is to find the problem(s). Put only one SAS statement on a line Use indentions to show the different parts of the program within DATA and PROC steps Use comment statements GENEROUSLY to document your code

Summer Know your colors Make sure that you are using the enhanced editor and know what code is generally what color (i.e. comments are green)

Summer Scroll Up Remember that your output and log windows are scrolled to the very bottom of the screen, scroll ALL the way up and check the whole thing. Look for common mistakes first (Semicolons and spelling errors!) Make sure you haven’t typed an ‘O’ where you want an ‘0’ or vice versa, this can cause SAS to think that your numeric or character variable should be change to the other variable type. SAS may do this automatically when you don’t want it done!

Summer What is wrong here? *Read the data file ToadJump.dat using a list input Data toads; Infile ‘c:MyRawData\ToadJump.dat’; Input ToadName$ Weight Jump1 Jump2 Jump3; Run;

Summer Here is the log window… ___________________________________________________________ *Read the data file ToadJump.dat using the list input Data toads; Infile ‘c:\MyRawData\ToadJump.dat’; ERROR : Statement is not valid or it is used out of proper order. Input ToadName$ Weight Jump1 Jump2 Jump3; ERROR : Statement is not valid or it is used out of proper order. Run; __________________________________________________________

Summer

Summer SAS is still running… You need to check the message above the menu on the Log window. If it says, as in this example, "DATA STEP running", then steps must be taken to stop the program from running. Even though SAS will continue to process other programs, results of such programs may be inaccurate, without any indication of syntax problems showing up in the log.

Summer SAS is still running… Several suggestions to stop the program are: Submit the following line: '; run; Submit the following line: *))%*'''))*/; If all else fails, exit SAS entirely (making sure that the revised program has been saved) and re-start it again.

Summer TPA Data Practice Go to the website and download the a TPA sample data set. Save it in a place that you can successfully write the Libname to point to! Either find a SAS program that you can change to fit the current problem or begin writing the code with a blank Editor page. The Goal: See how much reproduction of Tables 1 and 2 from the published paper you can recreate with your sample. We will practice Proc Boxplot together.