BookmarkSubscribeRSS Feed
EsbenVibel
Calcite | Level 5

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?

Thanks in advance!

Best,

Esben Vibel

8 REPLIES 8
Ksharp
Super User

How many obs you need to backward moving ?

EsbenVibel
Calcite | Level 5

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

Ksharp
Super User

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

EsbenVibel
Calcite | Level 5

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

    Ksharp
    Super User

    I have no problem. did you lost _temporary_ ?

    EsbenVibel
    Calcite | Level 5

    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

    Ksharp
    Super User

    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

    EsbenVibel
    Calcite | Level 5

    Thanks a lot! It works perfectly.

    sas-innovate-2024.png

    Don't miss out on SAS Innovate - Register now for the FREE Livestream!

    Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

     

    Register now!

    What is Bayesian Analysis?

    Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

    Find more tutorials on the SAS Users YouTube channel.

    Click image to register for webinarClick image to register for webinar

    Classroom Training Available!

    Select SAS Training centers are offering in-person courses. View upcoming courses for:

    View all other training opportunities.

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