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

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

 

Mruizv_1-1651324803730.png

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

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;

View solution in original post

3 REPLIES 3
FreelanceReinh
Jade | Level 19

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;
Mruizv
Obsidian | Level 7

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

FreelanceReinh
Jade | Level 19

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

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 3 replies
  • 2535 views
  • 3 likes
  • 2 in conversation