BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
DartRodrigo
Lapis Lazuli | Level 10

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

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Hi @DartRodrigo,

 

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

4 REPLIES 4
PaigeMiller
Diamond | Level 26

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
Astounding
PROC Star

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.

FreelanceReinh
Jade | Level 19

Hi @DartRodrigo,

 

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;

 

 

DartRodrigo
Lapis Lazuli | Level 10

Thanks for the answers Smiley Wink

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!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 1356 views
  • 2 likes
  • 4 in conversation