Solved
Contributor
Posts: 32

# How to use retain statement

Hi

I Have dataset as

subjectdate
100101052010
100101052010
100106012010
100205012010
100206012010
100220012010
100220012010
100220012010

I Want the Dataset to be

SubjectDateResult
100101052010Day1 Dose1
100101052010Day1 Dose1
100106012010Day2 Dose1
100205012010Day1 Dose1
100206012010Day2 Dose2
100220012010Day3 Dose1
100220012010Day3 Dose1
100220012010Day3 Dose1

I Want to check the output using Retain statement. and the Condition should be ( if Firts.subject and first.date then day1 dose 1 like that and last.subject and last.sate then day3 dose3)

Accepted Solutions
Solution
‎03-25-2013 10:55 AM
Super User
Posts: 23,666

## Re: How to use retain statement

It sounds like you're only counting days, not doses really?

you should be able to tweak this to get what you want.

data want;

set have;

retain day dose;

by subject date;

if first.subject then do;

day=0;

dose=0;

end;

if first.date then do;

dose=dose+1;

day=day+1;

end;

var="Day " ||day|| "Dose " || dose;

run;

All Replies
Super User
Posts: 23,666

## Re: How to use retain statement

Why is the first record for Day 2 Dose 2 while the first for day3 is dose 1?

Contributor
Posts: 32

## Re: How to use retain statement

No the output should be for the one person on the first date his result should be day1 and dose1 and second it shold be day2 dose 1 and third day it should be day3 dose1 like that

for the first subject it has only two days but for the second subject it has 3 days

Solution
‎03-25-2013 10:55 AM
Super User
Posts: 23,666

## Re: How to use retain statement

It sounds like you're only counting days, not doses really?

you should be able to tweak this to get what you want.

data want;

set have;

retain day dose;

by subject date;

if first.subject then do;

day=0;

dose=0;

end;

if first.date then do;

dose=dose+1;

day=day+1;

end;

var="Day " ||day|| "Dose " || dose;

run;

🔒 This topic is solved and locked.