BookmarkSubscribeRSS Feed
Lionfish
Calcite | Level 5
Hello all,

I am trying to figure out how I would be able to select observations from a data set so that I may do some calculations. Might be easier if I show you the coding, so here it goes:


Data compare;
do i=1 to 10;
xt=0.7*rannor(0);;
if xt GE 0 then xt=1;
else xt=0;
output;
end;
run;

proc print data=compare;
ID i;
run;

So I end up with a data set of xt with 10 observations.

What I would like to do is find the cumulative sum of the xt observations (is it xsum + xt?), the sum of the first and last observation in the set xt (not manually, my actual data set has 1000+ points), and the sum of the pairs x0*x1, x1*x2 (otherwise noted as x(i)*x(i-1) in math terms). After that I'd like to use a formula with all those sums.

I've tried making an array, but I end up with a nxn table rather than a nx1 style table. I'm rather new at this so I am not sure if there is a way to do this.

Any ideas?

Thanks in advance.
3 REPLIES 3
SPR
Quartz | Level 8 SPR
Quartz | Level 8
Hello Lionfish,

This is a solution below. Sum and fisrt/last are in the STAT dataset, products are in the R dataset. I've changed your compare dataset for debugging purposes.
[pre]
data compare;
do i=1 to 10;
xt=i**2;
output;
end;
run;
data _null_;
set compare end=e;
xsum+xt;
if _n_=1 then call SYMPUTX ('firstx',xt);
if e then do;
call SYMPUTX ('xsum',xsum);
call SYMPUTX ('lastx',xt);
end;
run;
%put xsum=&xsum firstx=&firstx lastx=&lastx;
data stat;
xsum=&xsum;
firstx=&firstx;
lastx=&lastx;
run;
data r;
set compare;
xij=xt*LAG(xt);
run;
[/pre]
Sincerely,
SPR
Lionfish
Calcite | Level 5
Hi there,

Thanks for the sol. I had did something completely different in the end, with arrays. I like what you did though, I believe those are some type of macro with the %? I should learn those, thanks again!

My code if interested:

[start]
data ZC;
do ts = 1 to 500;
z = 0;
array a(1050) X01-X1050;
do i=1 to 1050;
a(i)=0.7*z + rannor(0);
if a(i) GE 0 then a(i)=1;
else a(i)=0;
S = sum(of X01-X1050);
H = sum(of X01 X1050);
R = (X02-X01)**2;
PhiZC = cos(constant('pi')*(2*S-2*R-H)/(1049));
MSEzc=(PhiZC-0.7)**2;
F = (sum(of X02-X1050))**2;
PhiLSE = R / F;
MSElse=(PhiLSE - 0.7)**2;
drop i ts;
end;
output;
end;
run;

proc means data = ZC;
var MSEzc MSElse;
run;
[end]
SPR
Quartz | Level 8 SPR
Quartz | Level 8
Hello Lionfish,

I recommend you to take a SAS macro course to understand my code. It is necessary to know this to effectively porgram in SAS.

Sincerely,
SPR

P.S. %put in this context outputs macrovariable values into LOG.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 3 replies
  • 763 views
  • 0 likes
  • 2 in conversation