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

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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