Hello all,
I'm new to SAS, and I am having some issues with calculating moving average based on dates and groupings within the dataset. Basically, I am trying to calculate the moving average for each observation based on the last 90 days. Each observation has a date. I also need to group them so that the moving average is only based on the group. In other words, if I was grouping it by fruits, apples would have it's only moving average and orages, etc.
I know I will need to sort the dataset first, then use a retain statement. I was actually thinking of doing a macro too. I started this, but can't seem to get any part of it to work. Can someone help me? I know how to do the sorting, but I also know how to get the average, but it's not grouping correctly. It is only giving me the value of the observation.
I then tried to do it with SQL, but it's not working either. this is what I came up with.
proc sql;
create table data.moving_avg as
select a.*, mean(basis) as moving
from xx
where date>date
group by product;
I'm new to SAS, and I am having some issues with calculating moving average based on dates and groupings within the dataset. Basically, I am trying to calculate the moving average for each observation based on the last 90 days. Each observation has a date. I also need to group them so that the moving average is only based on the group. In other words, if I was grouping it by fruits, apples would have it's only moving average and orages, etc.
I know I will need to sort the dataset first, then use a retain statement. I was actually thinking of doing a macro too. I started this, but can't seem to get any part of it to work. Can someone help me? I know how to do the sorting, but I also know how to get the average, but it's not grouping correctly. It is only giving me the value of the observation.
I then tried to do it with SQL, but it's not working either. this is what I came up with.
proc sql;
create table data.moving_avg as
select a.*, mean(basis) as moving
from xx
where date>date
group by product;