DATA Step, Macro, Functions and more

Iteratively Generate a variable

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 12
Accepted Solution

Iteratively Generate a variable

Hello,

 

If I have two variables called xvar and yvar, how can I generate the yvar by adding 1 if xvar is even and staying the same if xvar is odd? The yvar is initialized to be 1.

 

xvar    yvar

2           1

3

5

6

7

8

 

Output wanted:

xvar    yvar

2           1

3           1

5           1

6           2

7           2

8           3

 

I tried the retain statement for yvar but can't get the expected result. Thanks.


Accepted Solutions
Solution
a month ago
Super User
Posts: 10,770

Re: Iteratively Generate a variable

Posted in reply to liyongkai800
data have;
input xvar; 
retain yvar 0;
if mod(xvar,2)=0 then yvar+1;   
cards;
2           
3
5
6
7
8
;
run;

View solution in original post


All Replies
Solution
a month ago
Super User
Posts: 10,770

Re: Iteratively Generate a variable

Posted in reply to liyongkai800
data have;
input xvar; 
retain yvar 0;
if mod(xvar,2)=0 then yvar+1;   
cards;
2           
3
5
6
7
8
;
run;
Occasional Contributor
Posts: 12

Re: Iteratively Generate a variable

Thank you, that's exactly what I expect.
☑ This topic is solved.

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

Discussion stats
  • 2 replies
  • 53 views
  • 1 like
  • 2 in conversation