Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Bring the Vampire out of the Shadows: Understanding the RETAIN and COUNT functions in SAS® Steve Black."— Presentation transcript:

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

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

3 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;

4 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_;

5 BY Statement Output

6 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;

7 By Age Output

8 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_.

9 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;

10 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;

11 RETAIN by COUNT

12 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.

13 Change from Baseline Data

14 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;

15 Change from Baseline Output

16 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.

17 Page Break Data

18 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;

19 Page Break Output

20 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.

21 Questions

22 Contact Information Name: Steve Black Company: Precision for Medicine Inc. City/State: Carlsbad CA Phone:


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

Similar presentations


Ads by Google