Code below. The DIFn return the dif value by GRP. But what I want is simple dif value by _N_.
How to do it?!
data temp;
do i=1 to 200;
grp=floor((i-1)/10);
output;
end;
run;quit;
data temp2;
set temp;
if i-floor(i/10)*10=5 then do;
_d=dif4(i);
end;
run;quit;
The DIF function, like the LAG function is a queue update function. In your IF statement
if i-floor(i/10)*10=5 then _d=dif4(i);
The queue is only updated at obs 5,15,15,25, 35,45,55,65,75,85, 95,.....,195. The first 4 times (5,15,...,35) the queue is updated the DIF4 function will return a missing value, because the queue needs 5 instances to fully populate with non-missing values. Note that the queue is NOT updated for all the other observations.
What you really want to do is update the queue at every observation, but keep the dif4 result only for obs 5,15,25,...195. This is what I think you want.
data temp;
do i=1 to 200;
grp=floor((i-1)/10);
output;
end;
run;
quit;
data temp2;
set temp;
_d=ifn(i-floor(i/10)*10=55,dif4(i),.);
run;
This will return a value of 4 for obs 5,15,,25, etc., and a missing value otherwise. That is because, unlike the IF statement, the IFN function always executes both the 2nd and 3rd arguments, regardless of which one it returns when assessing whether the first argument is true. (It returns the 2nd arg when the first arg is true, otherwise it returns the 3rd arg).
Because the IFN function will update the DIF4 queue for every obs (not just every 10th obs as in the IF statement) you will be subtracting the 4th previous obs (not the 40th preceding obs) from the current obs but only keeping the result for every 10th obs.
There are times when it is a good idea to put a DIF or LAG function in an IF statement. But this is not such a time.
_d shows up only on _N_=45/55/65... with value of 40.
What expected is to show up at 5/15/25... with value of 4
Could you please provide more details?:
*** USES i TO CALCULATE GROUPS ***;
DATA TEMP;
DO i = 1 TO 200;
GROUP = FLOOR( (i-1)/10 ); * max(GROUP) is 19;
OUTPUT;
END:
RUN:
*** WHAT DOES THIS STEP DO? ***;
* not using the GROUP variable;
DATA TEMP2;
SET TEMP;
IF i - FLOOR (i/10) * 10= 5 THEN DO;
* i - FLOOR (i/10) = 0.5? ;
_D = DIF4( i ); * what is DIF4( )? ;
END;
RUN;
create a column _d, based on condition [here mod(i,10)], keep DIFn on i[n changes on mod(i,10)].
Say mod(i,10)=5 to get DIF4(i), mod(i,10)=8 to get DIF3(i)... otherwise _D=.;
The DIF function, like the LAG function is a queue update function. In your IF statement
if i-floor(i/10)*10=5 then _d=dif4(i);
The queue is only updated at obs 5,15,15,25, 35,45,55,65,75,85, 95,.....,195. The first 4 times (5,15,...,35) the queue is updated the DIF4 function will return a missing value, because the queue needs 5 instances to fully populate with non-missing values. Note that the queue is NOT updated for all the other observations.
What you really want to do is update the queue at every observation, but keep the dif4 result only for obs 5,15,25,...195. This is what I think you want.
data temp;
do i=1 to 200;
grp=floor((i-1)/10);
output;
end;
run;
quit;
data temp2;
set temp;
_d=ifn(i-floor(i/10)*10=55,dif4(i),.);
run;
This will return a value of 4 for obs 5,15,,25, etc., and a missing value otherwise. That is because, unlike the IF statement, the IFN function always executes both the 2nd and 3rd arguments, regardless of which one it returns when assessing whether the first argument is true. (It returns the 2nd arg when the first arg is true, otherwise it returns the 3rd arg).
Because the IFN function will update the DIF4 queue for every obs (not just every 10th obs as in the IF statement) you will be subtracting the 4th previous obs (not the 40th preceding obs) from the current obs but only keeping the result for every 10th obs.
There are times when it is a good idea to put a DIF or LAG function in an IF statement. But this is not such a time.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.