BookmarkSubscribeRSS Feed
csetzkorn
Lapis Lazuli | Level 10

Looking at the code taken from here to calculate the moving average for one dimension/category patient:

 

 

data ds1;
  do patient='A','B','C';
    do month=1 to 7;
      num=int(ranuni(0)*10);
      output;
      end;
    end;
run;

proc sort;
  by patient;
run;

%let n = 4;

data ds2;
  set ds1;
  by patient;
  retain num_sum 0;
  if first.patient then do;
    count=0;
    num_sum=0;
  end;
  count+1;
  last&n=lag&n(num);
  if count gt &n then num_sum=sum(num_sum,num,-last&n);
  else num_sum=sum(num_sum,num);
  if count ge &n then mov_aver=num_sum/&n;
  else mov_aver=.;
run;

title 'Moving average within BY-Group';
proc print;
run;

 

I am wondering if this could be adapted for an additional dimenension/category Cat1. A starting point would be:

 

 

data ds1;
  do patient='A','B','C';
  	do Cat1='C1','C2';
	    do month=1 to 7;
	      num=int(ranuni(0)*10);
	      output;
	    end;
	end;
  end;
run;

proc sort;
  by patient Cat1;
run;

Any help would be very much appreciated. Thanks a lot.

3 REPLIES 3
Reeza
Super User

1. Change BY statement in SORT (done & shown) and Data Step (DS2)

2. Any time you see a reference to first.patient change it to first.cat1

 

I think that's all...but untested.

 


@csetzkorn wrote:

Looking at the code taken from here to calculate the moving average for one dimension/category patient:

 

 

data ds1;
  do patient='A','B','C';
    do month=1 to 7;
      num=int(ranuni(0)*10);
      output;
      end;
    end;
run;

proc sort;
  by patient;
run;

%let n = 4;

data ds2;
  set ds1;
  by patient;
  retain num_sum 0;
  if first.patient then do;
    count=0;
    num_sum=0;
  end;
  count+1;
  last&n=lag&n(num);
  if count gt &n then num_sum=sum(num_sum,num,-last&n);
  else num_sum=sum(num_sum,num);
  if count ge &n then mov_aver=num_sum/&n;
  else mov_aver=.;
run;

title 'Moving average within BY-Group';
proc print;
run;

 

I am wondering if this could be adapted for an additional dimenension/category Cat1. A starting point would be:

 

 

data ds1;
  do patient='A','B','C';
  	do Cat1='C1','C2';
	    do month=1 to 7;
	      num=int(ranuni(0)*10);
	      output;
	    end;
	end;
  end;
run;

proc sort;
  by patient Cat1;
run;

Any help would be very much appreciated. Thanks a lot.


 

csetzkorn
Lapis Lazuli | Level 10
Thanks. Are you sure the sort has only to be done for one dimension? Please note that the data generation code is just an example and the data can be in any order. Ultimately the MA is to be applied at the time dimension month.
Reeza
Super User

What do you mean the sort is by one dimension? Your proc sort showed two variables in the BY statement. That's what you need. In both BY statements, the data step and sort. 

 

And the fastest way is to answer this is to test it out 🙂

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 3 replies
  • 632 views
  • 0 likes
  • 2 in conversation