## Retain or Lag for a certain amount of observations

Solved
Occasional Contributor
Posts: 11

# Retain or Lag for a certain amount of observations

Hi,

I would like to retain a certain observation in SAS for a certain amount of observations forward.

If a criteria is met (date=announcement_date) then retain the value for the next 60 observations.

I don't want to write 60 lags but also make it flexible and change the 60 to 30 or whatever.

Best,
M

Accepted Solutions
Solution
‎09-02-2017 09:52 AM
PROC Star
Posts: 8,150

## Re: Retain or Lag for a certain amount of observations

I set the iterations to 6 in the following example, but that could be changed to 30, 60 or whatever you want:

```data want (drop=counter);
set have;
retain dummy;
if missing(counter) then do;
if date eq announcement_date then do;
dummy=x;
counter=0;
end;
end;
else do;
counter+1;
if date eq announcement_date then do;
dummy=x;
counter=0;
end;
if counter eq 6 then do;
call missing(counter);
call missing(dummy);
end;
end;
run;
```

Art, CEO, AnalystFinder.com

All Replies
PROC Star
Posts: 8,150

## Re: Retain or Lag for a certain amount of observations

After you've initially found a record that met your criterion and are retaining it for the next 60 observations, do you need to continue to check that criterion on the subsequent records? In short, tell us more about what you are trying to accomplish.

Art, CEO, AnalystFinder.com

Occasional Contributor
Posts: 11

## Re: Retain or Lag for a certain amount of observations

I want to establish an event study and one data set as stocks, dates and prices. the second one has stocks and announcement dates.

So basically always when date=announcement_date I want to fill in an additional (dummy variable) with just ones into the next 60 rows.

Solution
‎09-02-2017 09:52 AM
PROC Star
Posts: 8,150

## Re: Retain or Lag for a certain amount of observations

I set the iterations to 6 in the following example, but that could be changed to 30, 60 or whatever you want:

```data want (drop=counter);
set have;
retain dummy;
if missing(counter) then do;
if date eq announcement_date then do;
dummy=x;
counter=0;
end;
end;
else do;
counter+1;
if date eq announcement_date then do;
dummy=x;
counter=0;
end;
if counter eq 6 then do;
call missing(counter);
call missing(dummy);
end;
end;
run;
```

Art, CEO, AnalystFinder.com

Occasional Contributor
Posts: 11

## Re: Retain or Lag for a certain amount of observations

Works like a charm! Thank you!!

☑ This topic is solved.