DATA Step, Macro, Functions and more

Calculations with multiple observations

Accepted Solution Solved
Reply
Contributor
Posts: 20
Accepted Solution

Calculations with multiple observations

Hi All,

 

I have data like below

 

Date          B            C        D          E

20141231 193.83  93.04  129.98  96.15
20150102 194.41  93.02  129.95  95.03
20150105 188.34  90.56  129.05  93.5
20150106 184.53  88.63  127.53  92.95
20150107 187.28  90.3    129.51  94.87
20150108 190.27  91.58  131.8   97.06
20150109 187.35  90.42  131.54  95.99
20150112 185.07  89.5    30.87    95.86
20150113 184.93  89.23  131.17  95.01

 

I need to calcute Score = B1*B2+C1*C2+D1*D2+E1*E2

 

How to perform this calculation in SAS, Please advise


Accepted Solutions
Solution
‎12-23-2015 11:27 AM
Trusted Advisor
Posts: 1,137

Re: Calculations with multiple observations

[ Edited ]

Try to get the next record adjacent to the current record and then use the formula.

 

data have;
input Date          B1            C1        D1          E1;
cards;
20141231 193.83  93.04  129.98  96.15
20150102 194.41  93.02  129.95  95.03
20150105 188.34  90.56  129.05  93.5
20150106 184.53  88.63  127.53  92.95
20150107 187.28  90.3    129.51  94.87
20150108 190.27  91.58  131.8   97.06
20150109 187.35  90.42  131.54  95.99
20150112 185.07  89.5    30.87    95.86
20150113 184.93  89.23  131.17  95.01
;

data want;
obss=_n_+1;
set have end=last; if obss<=nobs then set have(keep=b1 c1 d1 e1 rename=(b1=b2 c1=c2 d1=d2 e1=e2)) end=last nobs=nobs point=obss;
if not last then score=sum(B1*B2,C1*C2,D1*D2,E1*E2);
else do;
score=.;
call missing(b2,c2,d2,e2) ;
end;
run;

 

Thanks,
Jag

View solution in original post


All Replies
Super User
Posts: 19,870

Re: Calculations with multiple observations

Your formula is correct and will work directly in SAS, by adding a semi colon.

What issue are you having?

Score = B1*B2+C1*C2+D1*D2+E1*E2;
Contributor
Posts: 20

Re: Calculations with multiple observations

 

 

 

Reeza,

 

 

I need to multiply first obervation with second and second with third and thrid with fourth.etc.and summing all the reult.

Super User
Posts: 19,870

Re: Calculations with multiple observations

Post the expected output from the data above. Are you looking forward or looking back - does it matter? Looking back is easier, you can use the lag function.

score=B*lag1(b)+c*lag(c) + d1*lag(d) +e*lag1(e);
Solution
‎12-23-2015 11:27 AM
Trusted Advisor
Posts: 1,137

Re: Calculations with multiple observations

[ Edited ]

Try to get the next record adjacent to the current record and then use the formula.

 

data have;
input Date          B1            C1        D1          E1;
cards;
20141231 193.83  93.04  129.98  96.15
20150102 194.41  93.02  129.95  95.03
20150105 188.34  90.56  129.05  93.5
20150106 184.53  88.63  127.53  92.95
20150107 187.28  90.3    129.51  94.87
20150108 190.27  91.58  131.8   97.06
20150109 187.35  90.42  131.54  95.99
20150112 185.07  89.5    30.87    95.86
20150113 184.93  89.23  131.17  95.01
;

data want;
obss=_n_+1;
set have end=last; if obss<=nobs then set have(keep=b1 c1 d1 e1 rename=(b1=b2 c1=c2 d1=d2 e1=e2)) end=last nobs=nobs point=obss;
if not last then score=sum(B1*B2,C1*C2,D1*D2,E1*E2);
else do;
score=.;
call missing(b2,c2,d2,e2) ;
end;
run;

 

Thanks,
Jag
Contributor
Posts: 20

Re: Calculations with multiple observations

Posted in reply to Jagadishkatam

Thanks Reeza and Jagadishkatam, Its working now. Hooray!

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 247 views
  • 2 likes
  • 3 in conversation