## Computation of backward moving percentiles

Occasional Contributor
Posts: 5

# Computation of backward moving percentiles

Hello everybody,

I work with a time series dataset, and I need to compute a backward moving percentile for a time series. How can this be done in SAS? I have considered the EXPAND procedure, but to my knowledge this procedure can only compute a backward moving median (i.e. 50% percentile) using the "MOVMED" statement. How should I proceed if I want to compute a (for example) 20% backward moving percentile using a time window of, say, 250 observations?

Best,

Esben Vibel

Super User
Posts: 10,761

## Re: Computation of backward moving percentiles

How many obs you need to backward moving ?

Occasional Contributor
Posts: 5

## Re: Computation of backward moving percentiles

My dataset has approx. 10,000 obs., and I would like to compute 20% backward moving percentiles based on a window of 252 observations.

Best,

Esben

Super User
Posts: 10,761

## Re: Computation of backward moving percentiles

I am not familiar with proc expand, maybe you can check the documention and find some one option.

And my code make a window of 252 includes the current obs.

```data have(drop=i);
do i=1 to 10000;
x=ranuni(-1);
output;
end;
run;
data want;
set have;
array a{0:251} _temporary_ ;
a{mod(_n_,252)}=x;
pct_20=pctl(20,of a{*});
run;

```

Ksharp

Occasional Contributor
Posts: 5

## Re: Computation of backward moving percentiles

When I run the last data step, the log writes the following error message:

ERROR: The ARRAYNAME

• specification requires a variable based array
• The error message refers to the last line (i.e. pct_20=pctl(20,of a{*}). Should this be changed somehow?

Esben

Super User
Posts: 10,761

## Re: Computation of backward moving percentiles

I have no problem. did you lost _temporary_ ?

Occasional Contributor
Posts: 5

## Re: Computation of backward moving percentiles

I simply copied your code directly into SAS and ran it. I use SAS 9.1; do I need newer version to run the code?

Esben

Super User
Posts: 10,761

## Re: Computation of backward moving percentiles

Are you still using sas 9.1?

```

data have(drop=i);
do i=1 to 10000;
x=ranuni(-1);
output;
end;
run;
data want(drop=aa:);
set have;
array a{0:251} aa1-aa252 ;
retain aa: ;
a{mod(_n_,252)}=x;
pct_20=pctl(20,of a{*});
run;

```

Ksharp

Occasional Contributor
Posts: 5

## Re: Computation of backward moving percentiles

Thanks a lot! It works perfectly.

Discussion stats
• 8 replies
• 684 views
• 1 like
• 2 in conversation