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

## Using one IF-ELSE condition for multiple variables

Hello, trying to figure out if there is a way to apply same If-Else condition to 2 variables at the same time

Current code:

``````DATA random1;
set agree;
random=ranuni(456);
RUN;

DATA random1;
set random1;
if random LT &cutoff1 then rater1='Yes';
else rater2='Yes';
if random GE &cutoff1 and random LT &cutoff2 then rater1='Yes';
else rater2='No';
if random GE &cutoff2 and random LT &cutoff3 then rater1='No';
else rater2='Yes';
if random GE &cutoff3  then rater1='No';
else rater2='No';
RUN;``````

I am getting No for rater2 at values less than 0.4 when it should be Yes and yes for rater2 at larger than 0.6 when it should be No

Cutoff1= 0.4 cutoff2=0.5 and cutoff3=0.6 currently

1 ACCEPTED SOLUTION

Accepted Solutions
Jade | Level 19

## Re: Using one IF-ELSE condition for multiple variables

Hello @Mruizv,

I think you want to randomly assign the four possible combinations of 'Yes' and 'No' to variables RATER1 and RATER2 with certain probabilities: ('Yes', 'Yes') and ('No', 'No') each with probability 0.4 and each of the other two combinations with probability 0.1.

One way to achieve this similarly to what you've tried is to use DO-END blocks containing the assignment statements for RATER1 and RATER2:

``````data random1;
set agree;
random=ranuni(456);
if random < &cutoff1 then do;
rater1='Yes';
rater2='Yes';
end;
else if random < &cutoff2 then do;
rater1='Yes';
rater2='No';
end;
else if random < &cutoff3 then do;
rater1='No';
rater2='Yes';
end;
else do;
rater1='No';
rater2='No';
end;
run;``````

(Note that this would replace your two DATA steps.)

But you could simplify the code as follows:

``````data random1;
set agree;
random=ranuni(456);
if random < &cutoff2 then rater1='Yes';
else rater1='No';
if random < &cutoff1 | &cutoff2 <= random < &cutoff3 then rater2='Yes';
else rater2='No';
run;``````

Alternatively, you could use a better random number generator plus the RAND function (with the "tabled distribution") creating random numbers 1, 2, 3, 4 with probabilities 0.4, 0.1, 0.1, 0.4:

``````data random1;
call streaminit(456);
set agree;
random=rand('table',0.4, 0.1, 0.1);
if random <= 2 then rater1='Yes';
else rater1='No';
if mod(random,2) then rater2='Yes';
else rater2='No';
run;``````

Often it's more convenient to use numeric variables with values 1 and 0 instead of character variables with values 'Yes' and 'No':

``````data random1;
call streaminit(456);
set agree;
random=rand('table',0.4, 0.1, 0.1);
rater1=(random <= 2);
rater2=mod(random,2);
run;``````
3 REPLIES 3
Jade | Level 19

## Re: Using one IF-ELSE condition for multiple variables

Hello @Mruizv,

I think you want to randomly assign the four possible combinations of 'Yes' and 'No' to variables RATER1 and RATER2 with certain probabilities: ('Yes', 'Yes') and ('No', 'No') each with probability 0.4 and each of the other two combinations with probability 0.1.

One way to achieve this similarly to what you've tried is to use DO-END blocks containing the assignment statements for RATER1 and RATER2:

``````data random1;
set agree;
random=ranuni(456);
if random < &cutoff1 then do;
rater1='Yes';
rater2='Yes';
end;
else if random < &cutoff2 then do;
rater1='Yes';
rater2='No';
end;
else if random < &cutoff3 then do;
rater1='No';
rater2='Yes';
end;
else do;
rater1='No';
rater2='No';
end;
run;``````

(Note that this would replace your two DATA steps.)

But you could simplify the code as follows:

``````data random1;
set agree;
random=ranuni(456);
if random < &cutoff2 then rater1='Yes';
else rater1='No';
if random < &cutoff1 | &cutoff2 <= random < &cutoff3 then rater2='Yes';
else rater2='No';
run;``````

Alternatively, you could use a better random number generator plus the RAND function (with the "tabled distribution") creating random numbers 1, 2, 3, 4 with probabilities 0.4, 0.1, 0.1, 0.4:

``````data random1;
call streaminit(456);
set agree;
random=rand('table',0.4, 0.1, 0.1);
if random <= 2 then rater1='Yes';
else rater1='No';
if mod(random,2) then rater2='Yes';
else rater2='No';
run;``````

Often it's more convenient to use numeric variables with values 1 and 0 instead of character variables with values 'Yes' and 'No':

``````data random1;
call streaminit(456);
set agree;
random=rand('table',0.4, 0.1, 0.1);
rater1=(random <= 2);
rater2=mod(random,2);
run;``````
Obsidian | Level 7

## Re: Using one IF-ELSE condition for multiple variables

The then do did the trick, I have the 3 variables as macro variables so the option of increments was not viable, but will keep it in the back pocket. Thank you

Jade | Level 19

## Re: Using one IF-ELSE condition for multiple variables

@Mruizv wrote:

I have the 3 variables as macro variables so the option of increments was not viable, ...

You could use your macro variables instead of the hardcoded probabilities:

``random=rand('table', &cutoff1, &cutoff2-&cutoff1, &cutoff3-&cutoff2);``
Discussion stats
• 3 replies
• 2753 views
• 3 likes
• 2 in conversation