## how many is the sum

dataset is abc: the name of column is A ;

A

4

5

6

if _N_= 3 then sum(lag(A)+lag2(A)+lag3(A) = ______

1 ACCEPTED SOLUTION

Accepted Solutions

## Re: how many is the sum

Nice trick question.

Since LAG3 is still missing when the third observation is processed (LAG has the value of the 2nd observation, LAG2 the value of the 1st), the result of the addition is missing, so the SUM function gets only one missing argument, and the result will also be missing.

But why don't you try it yourself? Too lazy?

4 REPLIES 4

## Re: how many is the sum

Nice trick question.

Since LAG3 is still missing when the third observation is processed (LAG has the value of the 2nd observation, LAG2 the value of the 1st), the result of the addition is missing, so the SUM function gets only one missing argument, and the result will also be missing.

But why don't you try it yourself? Too lazy?

## Re: how many is the sum

I just want to know why.
You give me the explain. Thank you.

## Re: how many is the sum

Depends on how you code it.

Check out this version:

``````data test;
input A;
v1 = sum(lag(a)+lag2(a)+lag3(a));
v2 = sum(lag(a),lag2(a),lag3(a));
if _n_=3 then v3 = sum(lag(a)+lag2(a)+lag3(a));
if _n_=3 then v4 = sum(lag(a)+lag2(a)+lag3(a));
put (_n_ A v1-v4) (=);
cards;
4
5
6
;``````

Results

Spoiler
```9    data test;
10     input A;
11     v1 = sum(lag(a)+lag2(a)+lag3(a));
12     v2 = sum(lag(a),lag2(a),lag3(a));
13     if _n_=3 then v3 = sum(lag(a)+lag2(a)+lag3(a));
14     if _n_=3 then v4 = sum(lag(a)+lag2(a)+lag3(a));
15     put (_n_ A v1-v4) (=);
16     cards;

_N_=1 A=4 v1=. v2=. v3=. v4=.
_N_=2 A=5 v1=. v2=4 v3=. v4=.
_N_=3 A=6 v1=. v2=9 v3=. v4=.
NOTE: Missing values were generated as a result of performing an operation on missing values.
Each place is given by: (Number of times) at (Line):(Column).
3 at 11:8    2 at 11:18   3 at 11:26   1 at 12:8    1 at 13:22   1 at 13:32   1 at 13:40   1 at 14:22   1 at 14:32
1 at 14:40
NOTE: The data set WORK.TEST has 3 observations and 5 variables.
NOTE: DATA statement used (Total process time):
real time           0.01 seconds
cpu time            0.01 seconds

20     ;
```