####
**Live Online Training :**
SAS Programming with 50+ Case Studies

- Explain Programming Concepts in Simple English

- Live Projects

- Scenario Based Questions

- Job Placement Assistance

- Get 10% off till Oct 26, 2017

- Batch starts from October 28, 2017

This tutorial explains how to use retain statement in SAS. In SAS, it's a very easy and useful way to retain values with RETAIN statement.

The following program creates a sample data for demonstration -

The

Suppose you need to generate a serial number (or row index number) with data step.

Suppose you need to calculate cumulative score. In financial data, we generally need to calculate cumulative score year to date.

Suppose you have a grouping variable say "region" and you need to generate a row index number by region.

Suppose you need to calculate cumulative sale by product categories.

The number of unique rows by a group can easily be calculated with PROC FREQ and PROC MEANS. The following program explains how we can calculate number of observations in a categorical variable with Data Step.

**Create Sample Data**The following program creates a sample data for demonstration -

data abcd;

input x y;

cards;

1 25

1 28

1 27

2 23

2 35

2 34

3 25

3 29

;

run;

**Uses of RETAIN Statement**The

**RETAIN**statement simply copies retaining values by telling the SAS not to reset the variables to missing at the beginning of each iteration of the DATA step. If you would not use retain statement then SAS would return missing at the beginning of each iteration.The retain statement keeps the value once assigned.

**Generate Serial Number**Suppose you need to generate a serial number (or row index number) with data step.

data aaa;

set abcd;

retain z 0;

z = z + 1;

run;

**We can retain implicitly by using the +1 notation.**data aaa;

set abcd;

z + 1;

run;

**Output Dataset**Output Data Set |

**Cumulative Score**Suppose you need to calculate cumulative score. In financial data, we generally need to calculate cumulative score year to date.

data aaa;

set abcd;

retain z 0;

z = z + y;

run;

Output Data Set |

**Generate Serial Number by Group**Suppose you have a grouping variable say "region" and you need to generate a row index number by region.

proc sort data = abcd;

by x;

run;

data aaa;

set abcd;

retain z;

if first.x then z = 1;

else z = z + 1;

by x;

run;

SAS : Retain Statement |

**Cumulative Score by Group**

Suppose you need to calculate cumulative sale by product categories.

data aaa1;

set aaa;

retain z1;

if first.x then z1 = y;

else z1 = z1 + y;

by x;

run;

**Number of Unique Observations**The number of unique rows by a group can easily be calculated with PROC FREQ and PROC MEANS. The following program explains how we can calculate number of observations in a categorical variable with Data Step.

data aaa2;

set abcd (drop = y);

retain z;

if first.x then z = 1;

else z = z + 1;

by x;

if last.x then output;

run;

Unique Count |

*Suppose you have more than 1 grouping variable*data temp;

input ID ID1 Score;

cards;

1 1 25

1 1 26

1 2 27

1 2 29

2 1 28

2 1 29

2 2 31

;

run;

data temp2;When you have more than 1 grouping variable, we can use multiple FIRST. statements with OR operator to generate serial numbers.

set temp;

by ID ID1;

if first.ID or first.ID1 then N = 1;

else N+1;

proc print;

run;

Hi,

ReplyDeleteThank you for making it so easy to understand took me just 1 min to understand the coding...great work brother.

Also can you please help me how can i prepare better for my base sas exam as it due in dec.

Check out this link - Base SAS Certification Questions and Answers

DeleteHi,

ReplyDeletethank you for your clear information.

please can you give me the answer for this question.

how can we find out cumulative totals for salary on each by group of gender?

Check the code below -

Deletedata abcd;

input Gender$ Salary;

cards;

M 25

M 26

M 27

F 23

F 24

F 25

;

run;

proc sort data = abcd;

by gender;

run;

data temp;

set abcd;

by gender;

retain tot ;

if first.gender then tot = salary;

else tot = tot + salary;

if last.gender;

drop salary;

run;

Hope it helps!

This is not what i want.

ReplyDeletePlease post your sample data with desired output. It's hard to guess the requirement. Thanks!

DeleteHi Deepanshu,

ReplyDeleteCould you please share Advance sas certification questions and answers as well..

Thanks!!