Getting Started with Arrays and DO LOOPS. Created 01/2004, Updated 03/2008, Afton Royal Training & Consulting

Similar documents
Text Input and Conditionals

Intro. Scheme Basics. scm> 5 5. scm>

Python for Analytics. Python Fundamentals RSI Chapters 1 and 2

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

The compiler is spewing error messages.

Introduction to Computer Science Midterm 3 Fall, Points

Introduction. C provides two styles of flow control:

1.7 Limit of a Function

They grow as needed, and may be made to shrink. Officially, a Perl array is a variable whose value is a list.

Loops / Repetition Statements. There are three loop constructs in C. Example 2: Grade of several students. Example 1: Fixing Bad Keyboard Input

Earthquake data in geonet.org.nz

While Loops CHAPTER 5: LOOP STRUCTURES. While Loops. While Loops 2/7/2013

Using an Array as an If-Switch Nazik Elgaddal and Ed Heaton, Westat, Rockville, MD

CISC-124. Casting. // this would fail because we can t assign a double value to an int // variable

CPSC 230 Extra review and solutions

Working with recursion. From definition to template. Readings: HtDP, sections 11, 12, 13 (Intermezzo 2).

Working with recursion

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02

LOOPS. Repetition using the while statement

CMSC201 Computer Science I for Majors

These are notes for the third lecture; if statements and loops.

Week 3. Topic 5 Asymptotes

PIC 10A Flow control. Ernest Ryu UCLA Mathematics

CS 370 Statements D R. M I C H A E L J. R E A L E F A L L

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

CS1 Lecture 3 Jan. 18, 2019

1: Introduction to Object (1)

REPETITION CONTROL STRUCTURE LOGO

CMSC201 Computer Science I for Majors

Chapter 6: Modifying and Combining Data Sets

CS1 Lecture 3 Jan. 22, 2018

CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch

Sets: Efficient Searching and Insertion

Algorithm Design and Recursion. Search and Sort Algorithms

Karlen Communications Citations and Bibliography in Word. Karen McCall, M.Ed.

Fundamentals. Fundamentals. Fundamentals. We build up instructions from three types of materials

(Refer Slide Time: 01:12)

LESSON 3 CONTROL STRUCTURES

Welcome! COMP s1. Programming Fundamentals

The Dynamic Typing Interlude

Storage class in C. Automatic variables External variables Static variables Register variables Scopes and longevity of above types of variables.

A control expression must evaluate to a value that can be interpreted as true or false.

CPSC 203 Extra review and solutions

Decisions, Decisions. Testing, testing C H A P T E R 7

Unit 3 Decision making, Looping and Arrays

The Problem With NODUPLICATES, Continued

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17

An Introduction to Python

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

The Progress Index - Not only how they work but why and when

Indicate the answer choice that best completes the statement or answers the question. Enter the appropriate word(s) to complete the statement.

Activity Guide - Will it Crash?

GIS 4653/5653: Spatial Programming and GIS. More Python: Statements, Types, Functions, Modules, Classes

Control Flow Structures

Repetition Structures

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

Anatomy of a Merge Gone Wrong James Lew, Compu-Stat Consulting, Scarborough, ON, Canada Joshua Horstman, Nested Loop Consulting, Indianapolis, IN, USA

CS 31: Introduction to Computer Systems. 03: Binary Arithmetic January 29

1 Lecture 5: Advanced Data Structures

XP: Backup Your Important Files for Safety

Loops / Repetition Statements

Repetition, Looping. While Loop

SE 3S03. Zahra Ali. Week of Feb 22, 2016

CS 4120 Lecture 31 Interprocedural analysis, fixed-point algorithms 9 November 2011 Lecturer: Andrew Myers

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

Solving Problems Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

CDs & DVDs: Easily Share Documents and Photos

Chapter 4: Conditionals and Loops

Search Lesson Outline

Iteration. # a and b are now equal # a and b are no longer equal Multiple assignment

HOW TO WRITE USER STORIES (AND WHAT YOU SHOULD NOT DO) Stuart Ashman, QA Director at Mio Global Bob Cook, Senior Product Development Manager, Sophos

CS193P - Lecture 3. iphone Application Development. Custom Classes Object Lifecycle Autorelease Properties

O(1) How long does a function take to run? CS61A Lecture 6

Chapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d.

Programming Basics and Practice GEDB029 Decision Making, Branching and Looping. Prof. Dr. Mannan Saeed Muhammad bit.ly/gedb029

Lab 4: Super Sudoku Solver CSCI 2101 Fall 2017

PLD Semester Exam Study Guide Dec. 2018

Week 1: Introduction to R, part 1

SE 3S03 - Tutorial 10. Helen Brown. Week of Mar 23, 2015

10 C Language Tips for Hardware Engineers

Lecture 7 Tao Wang 1

Iteration. CSE / ENGR 142 Programming I. Chapter 5. Motivating Loops. What s Wrong with Fahrenheit/Celsius Program? One More Type of Control Flow

How to Get Your Inbox to Zero Every Day

Using EnScript to Make Your Life Easier Session 1. Suzanne Widup, James Habben, Bill Taroli

Control of Flow. There are several Python expressions that control the flow of a program. All of them make use of Boolean conditional tests.

Design Phase. Create a class Person. Determine the superclass. NSObject (in this case)

Control Structures. Important Semantic Difference

Conditionals & Loops /

CS Summer 2013

CSCI-1200 Data Structures Spring 2018 Lecture 10 Vector Iterators & Linked Lists

Functions, Scope & Arguments. HORT Lecture 12 Instructor: Kranthi Varala

Ch. 12: Operator Overloading

Functions that return lists

Converting a Lowercase Letter Character to Uppercase (Or Vice Versa)

Introduction to C++ General Rules, Conventions and Styles CS 16: Solving Problems with Computers I Lecture #2

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Chapter 6. Loops. Iteration Statements. C s iteration statements are used to set up loops.

Testing and Debugging

Transcription:

Getting Started with Arrays and DO LOOPS Created 01/2004, Updated 03/2008, Afton Royal Training & Consulting

What to Expect We have 30 minutes to learn how to use DO LOOPS and arrays. We will assume you already know the basic data step techniques to prepare your data, and that you have some goal of what you will do with it later. First, we ll go over the different uses of DO. Second, we ll look at how to create an array. Third, we ll learn how to use one dimensional arrays, and see a list of resources for further reading.

Section 1: DO, DO WHILE, DO UNTIL

The DO Statement A DO statement designates a group of other statements to be run by SAS. An END statement shows where the group ends you will get an error and no results if you leave it out. DO statements can be nested be careful to match up the END statements correctly. Normally, the DO is in the middle of the statement, after a condition, but with arrays this is not as common.

Example: DO Statement data one.good one.bad; set one.all; if risk GE 8 then do; RiskCategory='High'; output one.bad; end; else if 1 GE risk LE 4 then do; RiskCategory='Low'; output one.good; end; else if 4<risk<8 then RiskCategory= Med ; run;

The Iterative DO Statement You can iterate, or loop, through a block of statements (DO through END) for a specified number of iterations, by adding an index variable specification. Because it doesn t matter what the index variable is called as long as you don t reuse the name of an existing variable by accident, most people use i this variable is saved in your data unless you drop it. The simplest and most common specification is i=1 to N with N as the number of iterations you want.

Examples: Iterative DO Most common: start to stop (by 1) do i=1 to 5; do i=6 to 10; do i=1 to limit; *variable*; do i=1 to count-1; *expression*; Unusual, but fine do i='one','two', four'; *char list*; do count=1,3,5,7,9; *num list*; do odds=1 to 9 by 2; *BY increment*; do i= 10 to -4 by -.5; *backwards*;

Infinite Looping Sometimes you will accidentally cause infinite looping, but it can be avoided. Be careful when changing the value of the index variable inside the loop. Think through the effects before running. Don t try to change the value of START during the looping, because it is evaluated when the loop begins. To get out of a DO loop, use LEAVE.

DO UNTIL and DO WHILE Statements After understanding the syntax of the simple DO and iterative DO statements, the syntax of the DO UNTIL and DO WHILE statements is simple to learn. The condition to be evaluated goes in parentheses after DO UNTIL or DO WHILE. The end is used as before. You must change the value of the index! The condition is evaluated 1. after the loop with UNTIL, 2. before the loop with WHILE.

Example: WHILE vs UNTIL *until evaluates after loop*; do until (x>=3); put x= 'until ' _n_=; x+1; *evaluates now*; end; *while evaluates before loop*; do while (p<=3); *evaluates now*; put p= 'while ' _n_=; p+1; end;

Summary: DO The DO statement designates a block of statements to execute (ending with end; ). The iterative DO statement executes the block multiple times, based on an index. DO WHILE evaluates a condition in parentheses, and then executes the block if the condition is true, repeatedly. DO UNTIL executes the block, then evaluates the condition and repeats the block if the condition is true (you always get at least 1).

Section 2: Array Syntax

Why Use an Array? When you need to do the same thing to a whole list of variables. When you find yourself cutting and pasting code many times, then changing the variable names. When you want to check the values of a group of variables that should be similar.

What is an Array? A temporary grouping of variables, in a specified order Only exists in the data step Does not carry over to the next data step The array itself is not a variable. The array must be defined before you can use it.

Defining an Array Use an ARRAY statement to define an array. It begins with the word array. Next, name the array (can t be a variable name, and shouldn t be a function name). Third, put the number of elements in parentheses of some sort. ARRAY name{sub} <element1 element2, >;

The Elements of an Array If you want the elements of the array (variables) to be named after the array itself, you don t need to list them. ARRAY Bob(3); *elements are Bob1, Bob2, Bob3*; If you want the elements to have any other names, you must list the variables in order. ARRAY Jim(3) Jim1 Jim6 JimBob; If the elements will be character variables, and they haven t yet been defined, you must add a $ after the array name and subscript. ARRAY Fred(2) $ FirstName LastName;

Initial Values and Temporary Arrays If you are creating array elements and want to initialize them with values, put the value list in parentheses after the element list. ARRAY Mary(2) $ MaryJane MaryBeth ( yes no ); When you are creating array elements that you won t need in the future, it is best to use a temporary array, which does not create variables. Add _TEMPORARY_ before the values. Do not list element names. The values are automatically retained. ARRAY Mary(2) $ _temporary_ ( yes no );

Using an Array After the ARRAY statement which creates the array, you can reference the array and it s elements easily. To reference the entire array, use the array name. To refer to a specific element of an array, use the index value(s) with the name Mary{1}. To refer to each of the elements of the array in turn (in an iterative DO loop), use an index variable with the name Mary{i}.

Example: One Array *Define the array first*; array stats(3) age weight height; *Use the array in an iterative DO loop*; do i=1 to 3; if stats(i)=0 then do; stats(i)=.; count+1; end; end;

Example: Two Arrays *Define the arrays first*; array stats(3) age weight height; array good(3) GoodAge GoodWt GoodHt; *Use the arrays in an iterative DO loop*; do i=1 to 3; if 0<stats(i)<900 then good(i)=1; end;

Resources The best resource to ask for help with SAS is the person who was doing your work before you borrow code when you can. Helpful reading (from http://v9doc.sas.com/sasdoc/): SAS Language Reference: Concepts Chapter 31, Array Processing SAS Language Reference: Dictionary look for do statement, and array statement