Returns values from a queue.
Category: | Special |
/***************************************************************************/ /* This program generates up to three lagged values. By increasing the */ /* size of the array and the number of assignment statements that use */ /* the LAGn functions, you can generate as many lagged values as needed. */ /***************************************************************************/ /* Create starting data. */ data old; input start end; datalines; 1 1 1 2 1 3 1 4 1 5 1 6 1 7 2 1 2 2 3 1 3 2 3 3 3 4 3 5 ; data new(drop=i count); set old; by start; /* Create and assign values to three new variables. Use ENDLAG1- */ /* ENDLAG3 to store lagged values of END, from the most recent to the */ /* third preceding value. */ array x(*) endlag1-endlag3; endlag1=lag1(end); endlag2=lag2(end); endlag3=lag3(end); /* Reset COUNT at the start of each new BY-Group */ if first.start then count=1; /* On each iteration, set to missing array elements */ /* that have not yet received a lagged value for the */ /* current BY-Group. Increase count by 1. */ do i=count to dim(x); x(i)=.; end; count + 1; run; proc print; run;
/* Title: Compute the moving average of a variable Goal: Compute the moving average of a variable through the entire data set, of the last n observations and of the last n observations within a BY-group. Input: */ data x; do x=1 to 10; output; end; run; /* Compute the moving average of the entire data set. */ data avg; retain s 0; set x; s=s+x; a=s/_n_; run; proc print; run; /* Compute the moving average of the last 5 observations. */ %let n = 5; data avg (drop=s); retain s; set x; s = sum (s, x, -lag&n(x)) ; a = s / min(_n_, &n); run; proc print; run; /* Compute the moving average within a BY-group of last n observations. For the first n-1 observations within the BY-group, the moving average is set to missing. */ 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; %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; proc print; run;