BookmarkSubscribeRSS Feed
PawelK
Calcite | Level 5

Hi All!

 

I want to create an additional column "death" in my data but based on probability. For example:

If male =0 then death= 0 with probability 0.3 and death=1 with probability 0.7

If male=1 then death= 0 with probability 0.4 and death=1 with probability 0.6

 

Could you please advise?

 

Thank you

 

EDIT:

What do you think about this:

data dane5;
set dane4;
x1=rand("uniform");
if male=0 then do;
if x1<=0.3 then death=1;
else death=0;
end;
if male=1 then do;
if x_1<=0.4 then death=1;
else death=0;
end;
run;

 

1 REPLY 1
ballardw
Super User

I think you may be looking for something like this:

 

      if male=1 then death=Rand('bernoulli',0.6);
      else if male=0 then death=Rand('bernoulli',0.7);

The bernoulli is a single observation, valued 1/0 where the probability of getting a 1 is specified as the parameter P of the function.

 

You can test this with this code. The Mean result of a 1/0 coded variable is the percent of 1's.

data example;
  do male=1,0;
   do trial=1 to 10000;
      if male=1 then death=Rand('bernoulli',0.6);
      else if male=0 then death=Rand('bernoulli',0.7);
      output;
   end;
  end;
run;

proc means data=example;
   class male;
   var death;
run;

Note that if your data doesn't do enough simulations the random nature means the result may not be really close to .7 or .6. Change the Trial=1 to 10; above and you can get result.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 960 views
  • 2 likes
  • 2 in conversation