Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Home
- /
- Programming
- /
- Programming
- /
- Using one IF-ELSE condition for multiple variables

Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Posted 04-30-2022 09:21 AM
(2752 views)

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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;
```

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

@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);`

Are you ready for the spotlight? We're accepting content ideas for **SAS Innovate 2025** to be held May 6-9 in Orlando, FL. The call is **open **until September 25. Read more here about **why** you should contribute and **what is in it** for you!

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.