## Retain value within data step

Solved
Occasional Contributor
Posts: 16

# Retain value within data step

Hi,

I have the following data set and would like to count the number of iterations on 'outcome' before values THIRDPARTY and RIGHTPARTY are true, i.e. on patid 1 (when sorted by date) the value of THIRDPARTY is met on the very first record so the count will be 1. I'd like to retain this value even though the condition is true on record #5 for the same ID. On padtid 2 the count will be 4 .. the idea is to retain the very first instance of either THIRDPARTY or RIGHTPARTY.

data dsn;

input patid calldate date9. outcome \$15. ;

datalines;

1 01JAN2012 THIRDPARTY

1 02JAN2012 NO

1 03JAN2012 NO

1 04JAN2012 THIRDPARTY

2 01JAN2012 NO

2 02JAN2012 NO

2 03JAN2012 NO

3 01JAN2012 NO

3 02JAN2012 THIRDPARTY

4 02JAN2012 THIRDPARTY

4 03JAN2012 NO

2 04JAN2012 RIGHTPARTY

2 05JAN2012 RIGHTPARTY

1 02JAN2012 NO

5 01JAN2012 NO

5 02JAN2012 NO

;

run;

data dsn2;

set dsn;

format calldate date9.;

run;

proc sort data = dsn2;

by patid calldate;

run;

Accepted Solutions
Solution
‎06-04-2012 12:14 PM
Valued Guide
Posts: 765

## Re: Retain value within data step

hi ... one way ...

data dsn;

input patid calldate :date9. outcome :\$15. ;

format calldate date9.;

datalines;

1 01JAN2012 THIRDPARTY

1 02JAN2012 NO

1 03JAN2012 NO

1 04JAN2012 THIRDPARTY

2 01JAN2012 NO

2 02JAN2012 NO

2 03JAN2012 NO

3 01JAN2012 NO

3 02JAN2012 THIRDPARTY

4 02JAN2012 THIRDPARTY

4 03JAN2012 NO

2 04JAN2012 RIGHTPARTY

2 05JAN2012 RIGHTPARTY

1 02JAN2012 NO

5 01JAN2012 NO

5 02JAN2012 NO

;

proc sort data=dsn;

by patid calldate;

run;

data want (keep=patid calldate when);

do j=1 by 1 until (last.patid);

set dsn;

by patid;

if missing(when) and find(outcome,'PARTY') then when = j;

end;

if missing(when) then call missing(calldate);

run;

All Replies
Super User
Posts: 8,111

## Re: Retain value within data step

Here is one way using the programming pattern known as a double DOW loop instead of the RETAIN function.

The variable COUNT will have the value you wanted.  The variable OBS will end up with the number of observations for that PATID.  If you do not need it then you can drop it.

data dsn;

input patid calldate date9. outcome \$15. ;

format calldate date9.;

datalines;

1 01JAN2012 THIRDPARTY

1 02JAN2012 NO

1 03JAN2012 NO

1 04JAN2012 THIRDPARTY

2 01JAN2012 NO

2 02JAN2012 NO

2 03JAN2012 NO

3 01JAN2012 NO

3 02JAN2012 THIRDPARTY

4 02JAN2012 THIRDPARTY

4 03JAN2012 NO

2 04JAN2012 RIGHTPARTY

2 05JAN2012 RIGHTPARTY

1 02JAN2012 NO

5 01JAN2012 NO

5 02JAN2012 NO

run;

proc sort;

by patid calldate;

run;

data want ;

do obs=1 by 1 until(last.patid);

set dsn ;

by patid calldate;

if count=. and outcome ne 'NO' then count=obs;

end;

do until(last.patid);

set dsn;

by patid calldate;

output;

end;

run;

Occasional Contributor
Posts: 16

## Re: Retain value within data step

Thanks Tom!!

This works and outputs the records I need!

Appreciate the swift response!!

Solution
‎06-04-2012 12:14 PM
Valued Guide
Posts: 765

## Re: Retain value within data step

hi ... one way ...

data dsn;

input patid calldate :date9. outcome :\$15. ;

format calldate date9.;

datalines;

1 01JAN2012 THIRDPARTY

1 02JAN2012 NO

1 03JAN2012 NO

1 04JAN2012 THIRDPARTY

2 01JAN2012 NO

2 02JAN2012 NO

2 03JAN2012 NO

3 01JAN2012 NO

3 02JAN2012 THIRDPARTY

4 02JAN2012 THIRDPARTY

4 03JAN2012 NO

2 04JAN2012 RIGHTPARTY

2 05JAN2012 RIGHTPARTY

1 02JAN2012 NO

5 01JAN2012 NO

5 02JAN2012 NO

;

proc sort data=dsn;

by patid calldate;

run;

data want (keep=patid calldate when);

do j=1 by 1 until (last.patid);

set dsn;

by patid;

if missing(when) and find(outcome,'PARTY') then when = j;

end;

if missing(when) then call missing(calldate);

run;

Occasional Contributor
Posts: 16

## Re: Retain value within data step

Thanks MikeZdeb! Perfect, exactly what I needed.

Thank you for responsding so quickly! Very helpful!

🔒 This topic is solved and locked.