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