DATA Step, Macro, Functions and more

Summarize columns

Accepted Solution Solved
Reply
Regular Contributor
Posts: 222
Accepted Solution

Summarize columns

Hi, i have an easy daubt.

 

But i forgot, how can i summarize columns var1 -- var5  like var1 = 10 var2 = 20 the next columns are the results summarizing of the two columns before.

 

just like:  

 

var1 var2 var3 

10    20    var1+var2  and so on.

 

This can be made in columns or lines:

var1 var2 var3 

10    20    var1+var2  and so on.

 

Lines

10

20

30

50

80

...

 

Thanks in advance


Accepted Solutions
Solution
‎12-02-2015 06:03 AM
Trusted Advisor
Posts: 1,259

Re: Summarize columns

Posted in reply to DartibaliRodrigo

Hi @DartibaliRodrigo,

 

I know that you are mathematically inclined. So, I guess you want to compute the Fibonacci numbers (with somewhat uncommon initial values, though).

 

Oh, I just see, @Astounding had the same understanding. (And, like me, he was also tempted by the LAG2 function.)

 

Here are my suggestions:

/* Fibonacci numbers, wide format */

data fibo_wide;
array a[25];
a1=1;
a2=1;
do n=3 to dim(a);
  a[n]=a[n-1]+a[n-2];
end;
drop n;
proc print width=min;
run;


/* Ditto with initial values 10, 20 */

data fibo_10_20_wide;
array a[25];
a1=10;
a2=20;
do n=3 to dim(a);
  a[n]=a[n-1]+a[n-2];
end;
drop n;
proc print width=min;
run;


/* Fibonacci numbers, long format */

data fibo_long;
i=1; a=1;
output;
do i=2 to 25;
  a+lag(a);
  output;
end;
proc print;
run;


/* Ditto with initial values 10, 20 */

data fibo_10_20_long;
a=10;
do i=1 to 25;
  a+lag(a);
  output;
end;
proc print;
var i a ;
run;


/* Ditto, but allowing for arbitrary initial values */

data fibo_10_20_long;
do i=1 to 25;
       if i=1 then a=10;
  else if i=2 then a=20;
  else a+lag_a;
  output;
  lag_a=lag(a);
end;
drop lag_a;
proc print;
run;


/* Ditto, but without IF-THEN-ELSE logic */

data fibo_10_20_long;
i=1; a=10; link sub;
i=2; a=20; link sub;
do i=3 to 25;
  a+lag_a;
  link sub;
end;
stop;

sub:
  output;
  lag_a=lag(a);
return;

drop lag_a;
proc print;
run;

 

 

View solution in original post


All Replies
Respected Advisor
Posts: 3,066

Re: Summarize columns

Posted in reply to DartibaliRodrigo

On the one hand, you describe adding columns within a row,

 

and then the data you provide has only one column and many rows.

 

It's not clear to me what you really are trying to do.

 

Can you clarify?

--
Paige Miller
Super User
Posts: 6,785

Re: Summarize columns

Posted in reply to DartibaliRodrigo

I think this is what you are asking for:

 

data want;

lines=10;

back1=0;

back2=0;

do i=1 to 20;

   back2 = back1;

   back1 = lines;

   lines + back2;

   output;

end;

keep lines;

run;

 

That would give you the first 20 values in the series.

 

Also note, usually a Fibonacci series repeats the first item in the series.  If that's what you want, this is untested, but should work:

 

data want;

lines=10;

do i=1 to 20;

   lines + lag2(lines);

   output;

end;

run;

 

It's unusual to use LAG in a loop, but I think it would work here.

 

 

Good luck.

Solution
‎12-02-2015 06:03 AM
Trusted Advisor
Posts: 1,259

Re: Summarize columns

Posted in reply to DartibaliRodrigo

Hi @DartibaliRodrigo,

 

I know that you are mathematically inclined. So, I guess you want to compute the Fibonacci numbers (with somewhat uncommon initial values, though).

 

Oh, I just see, @Astounding had the same understanding. (And, like me, he was also tempted by the LAG2 function.)

 

Here are my suggestions:

/* Fibonacci numbers, wide format */

data fibo_wide;
array a[25];
a1=1;
a2=1;
do n=3 to dim(a);
  a[n]=a[n-1]+a[n-2];
end;
drop n;
proc print width=min;
run;


/* Ditto with initial values 10, 20 */

data fibo_10_20_wide;
array a[25];
a1=10;
a2=20;
do n=3 to dim(a);
  a[n]=a[n-1]+a[n-2];
end;
drop n;
proc print width=min;
run;


/* Fibonacci numbers, long format */

data fibo_long;
i=1; a=1;
output;
do i=2 to 25;
  a+lag(a);
  output;
end;
proc print;
run;


/* Ditto with initial values 10, 20 */

data fibo_10_20_long;
a=10;
do i=1 to 25;
  a+lag(a);
  output;
end;
proc print;
var i a ;
run;


/* Ditto, but allowing for arbitrary initial values */

data fibo_10_20_long;
do i=1 to 25;
       if i=1 then a=10;
  else if i=2 then a=20;
  else a+lag_a;
  output;
  lag_a=lag(a);
end;
drop lag_a;
proc print;
run;


/* Ditto, but without IF-THEN-ELSE logic */

data fibo_10_20_long;
i=1; a=10; link sub;
i=2; a=20; link sub;
do i=3 to 25;
  a+lag_a;
  link sub;
end;
stop;

sub:
  output;
  lag_a=lag(a);
return;

drop lag_a;
proc print;
run;

 

 

Regular Contributor
Posts: 222

Re: Summarize columns

Posted in reply to FreelanceReinhard

Thanks for the answers Smiley Wink

🔒 This topic is solved and locked.

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

Discussion stats
  • 4 replies
  • 318 views
  • 2 likes
  • 4 in conversation