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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
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

2 REPLIES 2
Ksharp
Super User
data have;
input xvar; 
retain yvar 0;
if mod(xvar,2)=0 then yvar+1;   
cards;
2           
3
5
6
7
8
;
run;
liyongkai800
Obsidian | Level 7
Thank you, that's exactly what I expect.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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