Bring the Vampire out of the Shadows: Understanding the RETAIN and COUNT functions in SAS® Steve Black.

Slides:



Advertisements
Similar presentations
Switch code for Lab 4.2 switch (input) { /* input is a variable that we will test. */ case 'M': printf("The prefix is equal to 1E6.\n"); break; case 'k':
Advertisements

How SAS implements structured programming constructs
DIVERSE REPORT GENERATION By Chris Speck PAREXEL International Durham, NC.
A guide to the unknown…  A dataset is longitudinal if it tracks the same type of information on the same subjects at multiple points in time or space.
Outline Proc Report Tricks Kelley Weston. Outline Examples 1.Text that spans columnsText that spans columns 2.Patient-level detail in the titlesPatient-level.
Tutorial 12 Working with Arrays, Loops, and Conditional Statements
Chapter 5: Loops and Files.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
SAS PROC REPORT PROC TABULATE
Introduction to SAS Essentials Mastering SAS for Data Analytics Alan Elliott and Wayne Woodward SAS ESSENTIALS -- Elliott & Woodward1.
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - JavaScript: Arrays Outline 11.1 Introduction 11.2 Arrays 11.3 Declaring and Allocating Arrays.
Lecture 4 PL/SQL language. PL/SQL – procedural SQL Allows combining procedural and SQL code PL/SQL code is compiled, including SQL commands PL/SQL code.
Learning to love the SAS LAG function Phuse 9-12 October 2011 Herman Ament, MSD, Oss NL Phuse 9-12 October 2011.
1 PhUSE 2011 Missing Values in SAS Magnus Mengelbier Director.
1 Back Up with Each Submit One approach for keeping a dynamic back up copy of your current work.
Dynamic Web Pages & JavaScript. Dynamic Web Pages Dynamic = Change Dynamic Web Pages are web pages that change. More than just moving graphics around.
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
Define your Own SAS® Command Line Commands Duong Tran – Independent Contractor, London, UK Define your Own SAS® Command Line Commands Duong Tran – Independent.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Priya Ramaswami Janssen R&D US. Advantages of PROC REPORT -Very powerful -Perform lists, subsets, statistics, computations, formatting within one procedure.
read and learn from example loop programs develop modular program
Creating and Using Custom Formats for Data Manipulation and Summarization Presented by John Schmitz, Ph.D. Schmitz Analytic Solutions, LLC Certified Advanced.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
Iteration. Iteration: Review  If you wanted to display all the numbers from 1 to 1000, you wouldn’t want to do this, would you? Start display 1 display.
1 Chapter 3: Getting Started with Tasks 3.1 Introduction to Task Dialogs 3.2 Creating a Listing Report 3.3 Creating a Frequency Report 3.4 Creating a Two-Way.
A leading global CRO 1 Effective Use of the RETAIN Statement in Programming Clinical Trial Mingxia Chen Biostatistician Beijing, China.
BMTRY 789 Lecture9: Proc Tabulate Readings – Chapter 11 & Selected SUGI Reading Lab Problems , 11.2 Homework Due Next Week– HW6.
Chapter 6: Modifying and Combining Data Sets  The SET statement is a powerful statement in the DATA step DATA newdatasetname; SET olddatasetname;.. run;
CSCE Do Loops Simplest form DO j = 1,100 PRINT *,j END DO Variable j runs from 1 to 100 counting by ones.
SAS ® 101 Based on Learning SAS by Example: A Programmer’s Guide Chapters 16 & 17 By Tasha Chapman, Oregon Health Authority.
Exsys Developer Tutorial
Chapter 11 - JavaScript: Arrays
Chapter 11 Reading SAS Data
Applied Business Forecasting and Regression Analysis
Chapter 6: Modifying and Combining Data Sets
29th Oct Review Session 8.
Error Handling Summary of the next few pages: Error Handling Cursors.
Intro to PHP & Variables
Siti Nurbaya Ismail Senior Lecturer
Chapter 18: Modifying SAS Data Sets and Tracking Changes
Using the Set Operators
Instructor: Raul Cruz-Cano
Tamara Arenovich Tony Panzarella
Introduction to Classes
Lab 2 Data Manipulation and Descriptive Stats in R
SAS Essentials How SAS Thinks
Introduction to TouchDevelop
Building Web Applications
Introduction to SAS A SAS program is a list of SAS statements executed in order Every SAS statement ends with a semicolon! SAS statements can be in caps.
Quick Data Summaries in SAS
Data Structures – 1D Lists
Defining and Calling a Macro
Iteration: Beyond the Basic PERFORM
How are your SAS Skills? Chapter 1: Accessing Data (Question # 1)
3 Iterative Processing.
Introduction to DATA Step Programming: SAS Basics II
Lab 3 and HRP259 Lab and Combining (with SQL)
Producing Descriptive Statistics
Creating BDS DERIVED Parameters for a Subject-level Frequency Summary Table? Then this macro can be useful.
Passing Simple and Complex Parameters In and Out of Macros
Parallel Processing in Base SAS
IMPORTING DATA WITH DDE (Dynamic Data Exchange) Or Proc Import?
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Loops.
Data Structures & Algorithms
In this class, we will cover:
Let’s continue to review some of the statistics you’ve learned in your first class: Bivariate analyses (two variables measured at a time on each observation)
Using C++ Arithmetic Operators and Control Structures
Introduction to SAS Essentials Mastering SAS for Data Analytics
Presentation transcript:

Bring the Vampire out of the Shadows: Understanding the RETAIN and COUNT functions in SAS® Steve Black

Introduction If you’ve ever felt like this when working with an Retain statement, then this presentation should help.

A Simple Way to Count Lots of Ways to Count in SAS data one; set sashelp.class; *** option 1 ***; new_var=_n_; *** option 2 ***; count+1; run;

Adding Some Complexity Using the BY Statement proc sort data=sashelp.class out=two; by sex; run; data three; set two; *** sets the first new value to null ***; if first.sex then count=.; *** starts counting by 1 by sex ***; count+1; *** just for fun let's see what this guy still does ***; new_var=_n_;

BY Statement Output

Let’s do it again by Age proc sort data=sashelp.class out=four; run;   data five; set four; if first.age then count=.; count+1;

By Age Output

The RETAIN Statement Definition: Causes a variable that is created by an INPUT or assignment statement to retain its value from one iteration of the DATA step to the next. A default value can be specified. You can also use it on a type of output such as _CHAR_ or _NUMERIC_ or _ALL_.

RETAIN If a variable is already created using the retain statement can be useful in setting the order in which they appear in a dataset. data _lab; retain subjid paramn param ady avisitn avisit; set adam.adlb;

RETAIN the COUNT data six; set four; by age;   *** holds the value of count and sets the default value ***; retain count 0; *** adds the value of count per each new value of age ***; if first.age then count=count+1; run;

RETAIN by COUNT

Real World Scenarios A Baseline Example A common issue: creating the change from baseline value in a dataset where there are multiple parameters and subject over a number of visits.

Change from Baseline Data

Change from Baseline Code data _new; set _labs; by subjid paramn ady avisitn;   retain base ; if first.paramn then base=.; if avisitn=0 then base=aval; run;

Change from Baseline Output

A Page Break Example Many times in creating a table I’ll need to set where the page breaks but done in groups and not per a set row. So many times I want all the data for a certain visit to be on a page and not break between. If the data is still ongoing there may not be some visits that have arrived and so doing thy page breaks dynamically is best.

Page Break Data

Page Break Code data final; set labs; by paramn avisitn y_axis; *** retain my counter var ***; retain cnt 0; *** create counter per each new avisitn ***; if first.avisitn then cnt=cnt+1; *** create new page counter per every 3 avisitns ***; xpage=ceil(cnt/3); run;

Page Break Output

Summary I’ve shown a simple way to count observation in the data step. I’ve shown how to use the by statement in counting. I’ve shown how to use the retain statement to count I’ve demonstrated how to use the by statement and the first. and last. functions. Provided two real world example of how I’ve found using the retain function useful.

Questions

Contact Information Name: Steve Black Company: Precision for Medicine Inc. City/State: Carlsbad CA Phone: 760-658-5919 Email: steve.black@precisionformedicine.com