turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- SAS Procedures
- /
- Observation Selection

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

04-03-2011 08:19 PM

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.

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

04-05-2011 11:23 AM

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

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

04-06-2011 08:26 AM

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]

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]

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

04-06-2011 10:29 AM

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.

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.