<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Assign value to variable based on probability in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Assign-value-to-variable-based-on-probability/m-p/817613#M322728</link>
    <description>&lt;P&gt;Hi All!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to create an additional column "death" in my data but based on probability. For example:&lt;/P&gt;&lt;P&gt;If male =0 then death= 0 with probability 0.3 and death=1 with probability 0.7&lt;/P&gt;&lt;P&gt;If male=1 then death= 0 with probability 0.4 and death=1 with probability 0.6&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Could you please advise?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;EDIT:&lt;/P&gt;&lt;P&gt;What do you think about this:&lt;/P&gt;&lt;PRE&gt;data dane5;&lt;BR /&gt;set dane4;&lt;BR /&gt;x1=rand("uniform");&lt;BR /&gt;if male=0 then do;&lt;BR /&gt;if x1&amp;lt;=0.3 then death=1;&lt;BR /&gt;else death=0;&lt;BR /&gt;end;&lt;BR /&gt;if male=1 then do;&lt;BR /&gt;if x_1&amp;lt;=0.4 then death=1;&lt;BR /&gt;else death=0;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 11 Jun 2022 21:36:39 GMT</pubDate>
    <dc:creator>PawelK</dc:creator>
    <dc:date>2022-06-11T21:36:39Z</dc:date>
    <item>
      <title>Assign value to variable based on probability</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assign-value-to-variable-based-on-probability/m-p/817613#M322728</link>
      <description>&lt;P&gt;Hi All!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to create an additional column "death" in my data but based on probability. For example:&lt;/P&gt;&lt;P&gt;If male =0 then death= 0 with probability 0.3 and death=1 with probability 0.7&lt;/P&gt;&lt;P&gt;If male=1 then death= 0 with probability 0.4 and death=1 with probability 0.6&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Could you please advise?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;EDIT:&lt;/P&gt;&lt;P&gt;What do you think about this:&lt;/P&gt;&lt;PRE&gt;data dane5;&lt;BR /&gt;set dane4;&lt;BR /&gt;x1=rand("uniform");&lt;BR /&gt;if male=0 then do;&lt;BR /&gt;if x1&amp;lt;=0.3 then death=1;&lt;BR /&gt;else death=0;&lt;BR /&gt;end;&lt;BR /&gt;if male=1 then do;&lt;BR /&gt;if x_1&amp;lt;=0.4 then death=1;&lt;BR /&gt;else death=0;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 11 Jun 2022 21:36:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assign-value-to-variable-based-on-probability/m-p/817613#M322728</guid>
      <dc:creator>PawelK</dc:creator>
      <dc:date>2022-06-11T21:36:39Z</dc:date>
    </item>
    <item>
      <title>Re: Assign value to variable based on probability</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assign-value-to-variable-based-on-probability/m-p/817616#M322730</link>
      <description>&lt;P&gt;I think you may be looking for something like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;      if male=1 then death=Rand('bernoulli',0.6);
      else if male=0 then death=Rand('bernoulli',0.7);
&lt;/PRE&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can test this with this code. The Mean result of a 1/0 coded variable is the percent of 1's.&lt;/P&gt;
&lt;PRE&gt;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;&lt;/PRE&gt;
&lt;P&gt;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.&lt;/P&gt;</description>
      <pubDate>Sat, 11 Jun 2022 22:02:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assign-value-to-variable-based-on-probability/m-p/817616#M322730</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-06-11T22:02:22Z</dc:date>
    </item>
  </channel>
</rss>

