BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
tianerhu
Pyrite | Level 9

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
Kurt_Bremser
Super User

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?

View solution in original post

4 REPLIES 4
Kurt_Bremser
Super User

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?

tianerhu
Pyrite | Level 9
I just want to know why.
You give me the explain. Thank you.
Tom
Super User Tom
Super User

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     ;
tianerhu
Pyrite | Level 9
Thank you for your help.

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 849 views
  • 1 like
  • 3 in conversation