BookmarkSubscribeRSS Feed
SwapnaKumari
Fluorite | Level 6

Hi,

Can anyone please help me to get SAS code to calculate moving median for last 30 days.

Regards,

Swapna

10 REPLIES 10
RW9
Diamond | Level 26 RW9
Diamond | Level 26

First Google hit should help (has full code):

25027 - Compute the moving average of a variable

SwapnaKumari
Fluorite | Level 6

Hi,

Thanks for your reply.

The link which you shared is to calculate moving average and I have already gone through it.. Smiley Happy

But I am looking for a code to calculate Moving "MEDIAN" for last 30 days.

Regards.

Swapna

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Ah. maybe have a look at the expand procedure then:

(Replace MOVAVE with the MOVMED)

22104 - How can I compute a moving average of a variable?

Per:

SAS/ETS(R) 9.2 User's Guide

SwapnaKumari
Fluorite | Level 6

Have gone to that option as well, and sorry to not mention that in my first post.

The challenge is we don't have SAS ETS with us, thus I need a code which should work in Base SAS.

Ksharp
Super User

If you have SAS9.4 , then SQL can get that , otherwise use ARRAY.

SwapnaKumari
Fluorite | Level 6

Hi Xia,

Yes I am having SAS 9.4, It would be really great if you can please share some sample code, so that I can try it at my end.

Regards,

Swapna

Reeza
Super User

SAS - moving min/max using arrays

You'll notice the code was originally sourced from this website.

Ksharp
Super User

OK. Here is an example :

data have;
do date='01jan2014'd to today();
v=ranuni(1234);
output;
end;
format date date9.;
run;


proc sql;
create table want as 
 select *,(select median(v) from have where date between a.date-30 and a.date) as moving_median_30_day
  from have as a;
 quit;

Xia Keshan

Reeza
Super User

Just a note that the SAS SQL Median function only works in SAS 9.4

Ksharp
Super User

Thanks Reeza. I have already mentioned it  before .

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 10 replies
  • 4323 views
  • 0 likes
  • 4 in conversation