BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
koya
Fluorite | Level 6

HI,

I have the below requirement and wondering if this can be implemented in simple datastep loop:

will be starting with a simple dataset with two variable

input:

x y
1 0

 

logic:

x y z
1 0 x+y
prev z prev y +1 x+y
prev z prev y +1 x+y

 

desired output:

x y z
1 0 1
1 1 2
2 2 4
4 3 7
7 4 11
1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Hi @koya,

 


@koya wrote:

... wondering if this can be implemented in simple datastep loop


Yes, this is possible. Wondering why you don't just start writing that data step.

Spoiler
data have;
x=1; y=0;
run;

data want;
set have;
do _n_=1 to 5;
  z=x+y;
  output;
  x=z; y+1;
end;
run;

/* Or even simpler: */

data want;
set have;
do y=y to 4;
  z=x+y;
  output;
  x=z;
end;
run;

View solution in original post

2 REPLIES 2
FreelanceReinh
Jade | Level 19

Hi @koya,

 


@koya wrote:

... wondering if this can be implemented in simple datastep loop


Yes, this is possible. Wondering why you don't just start writing that data step.

Spoiler
data have;
x=1; y=0;
run;

data want;
set have;
do _n_=1 to 5;
  z=x+y;
  output;
  x=z; y+1;
end;
run;

/* Or even simpler: */

data want;
set have;
do y=y to 4;
  z=x+y;
  output;
  x=z;
end;
run;
Ksharp
Super User
data want;
 x=1;y=0;z=x+y;output;
 do i=1 to 10;
   x=z;y=y+1;z=x+y;output;
 end;
 drop i;
run;

proc print;run;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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