<?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 Re: Generating binary variables in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Generating-binary-variables/m-p/28747#M6723</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;hi ... not IML, just a couple data steps ...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: 'courier new', courier;"&gt;* 60 observations, x ranges from 1 to 5;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: 'courier new', courier;"&gt;data x;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: 'courier new', courier;"&gt;do _n_ = 1 to 60;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: 'courier new', courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; x = ceil(5*ranuni(0));&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: 'courier new', courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;output;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: 'courier new', courier;"&gt;end;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: 'courier new', courier;"&gt;run;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: 'courier new', courier;"&gt;* add binary variables based on value of x;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: 'courier new', courier;"&gt;data x;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: 'courier new', courier;"&gt;array b(5) (5*0);&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: 'courier new', courier;"&gt;set x;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: 'courier new', courier;"&gt;b(x) = 1;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: 'courier new', courier;"&gt;output;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: 'courier new', courier;"&gt;b(x) = 0;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: 'courier new', courier;"&gt;run;&lt;/STRONG&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 11 Jan 2012 22:09:41 GMT</pubDate>
    <dc:creator>MikeZdeb</dc:creator>
    <dc:date>2012-01-11T22:09:41Z</dc:date>
    <item>
      <title>Generating binary variables</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Generating-binary-variables/m-p/28738#M6714</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I need to generate 5 binary variables but&amp;nbsp; each observation must take 1 in one variable and zero in the other 4 variables.I need the code for this case&lt;/P&gt;&lt;P&gt;Can anyone help in this case &lt;/P&gt;&lt;P&gt;Thanks in advance&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 11 Jan 2012 20:36:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Generating-binary-variables/m-p/28738#M6714</guid>
      <dc:creator>heba2000</dc:creator>
      <dc:date>2012-01-11T20:36:00Z</dc:date>
    </item>
    <item>
      <title>Generating binary variables</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Generating-binary-variables/m-p/28739#M6715</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Within data step:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt; Set all five binary variables to zero;&lt;/P&gt;&lt;P&gt;Then use &lt;STRONG&gt;select&lt;/STRONG&gt; construct or &lt;STRONG&gt;if-then else &lt;/STRONG&gt;construct or other appropriate strategy to assign 1 to the appropriate binary variable.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Hope that helps.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 11 Jan 2012 20:46:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Generating-binary-variables/m-p/28739#M6715</guid>
      <dc:creator>LarryWorley</dc:creator>
      <dc:date>2012-01-11T20:46:11Z</dc:date>
    </item>
    <item>
      <title>Generating binary variables</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Generating-binary-variables/m-p/28740#M6716</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt; but in this case only the first two variables will be zero and 1 but the other 3 all zeros because the condition for the second variable would be =0 if var1=1 and =1 if var1=0 so I canot use this &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 11 Jan 2012 20:50:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Generating-binary-variables/m-p/28740#M6716</guid>
      <dc:creator>heba2000</dc:creator>
      <dc:date>2012-01-11T20:50:14Z</dc:date>
    </item>
    <item>
      <title>Generating binary variables</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Generating-binary-variables/m-p/28741#M6717</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt; I am assuming you have an underlying variable with 5 levels or categories, then you could try this: &lt;/P&gt;&lt;P&gt;Assuming a pre-existing variable to generate the binary variables from:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Suppose x can be equal to 1, 2, 3, 4 or 5.&amp;nbsp; Then do something like this:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;b1 = 0 ;&lt;/P&gt;&lt;P&gt;b2 = 0 ;&lt;/P&gt;&lt;P&gt;b3 = 0 ;&lt;/P&gt;&lt;P&gt;b4 = 0;&lt;/P&gt;&lt;P&gt;b5 = 0;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Select (x) ;&lt;/P&gt;&lt;P&gt;&amp;nbsp; when (1) b1 = 1 ;&lt;/P&gt;&lt;P&gt;&amp;nbsp; when (2) b2 = 1 ;&lt;/P&gt;&lt;P&gt;&amp;nbsp; when (3) b3 = 1 ;&lt;/P&gt;&lt;P&gt;&amp;nbsp; when (4) b4 = 1 ;&lt;/P&gt;&lt;P&gt;&amp;nbsp; when (5) b5 = 1;&lt;/P&gt;&lt;P&gt;end ;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;so you will end up with data set like this:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;x&amp;nbsp;&amp;nbsp; b1 b2 b3 b4 b5&lt;/P&gt;&lt;P&gt;1&amp;nbsp;&amp;nbsp; 1&amp;nbsp;&amp;nbsp; 0&amp;nbsp;&amp;nbsp; 0&amp;nbsp; 0&amp;nbsp;&amp;nbsp; 0&lt;/P&gt;&lt;P&gt;2&amp;nbsp;&amp;nbsp; 0&amp;nbsp; 1&amp;nbsp;&amp;nbsp;&amp;nbsp; 0&amp;nbsp; 0&amp;nbsp;&amp;nbsp; 0&lt;/P&gt;&lt;P&gt;3&amp;nbsp;&amp;nbsp; 0&amp;nbsp;&amp;nbsp; 0&amp;nbsp;&amp;nbsp; 1&amp;nbsp; 0&amp;nbsp;&amp;nbsp; 0&lt;/P&gt;&lt;P&gt;4&amp;nbsp;&amp;nbsp; 0&amp;nbsp;&amp;nbsp; 0&amp;nbsp;&amp;nbsp; 0&amp;nbsp; 1&amp;nbsp;&amp;nbsp; 0&lt;/P&gt;&lt;P&gt;5&amp;nbsp;&amp;nbsp; 0&amp;nbsp;&amp;nbsp; 0&amp;nbsp;&amp;nbsp; 0&amp;nbsp; 0&amp;nbsp;&amp;nbsp; 1&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If instead you want the binary variables based on line numbers, you could do some like this:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;if mod(_n_,5) = 0 then x = 5 ;&lt;/P&gt;&lt;P&gt;else x = mod(_n_,5) ;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;then add code from above.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If this does not do it, I am really confused about what you are trying to do.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 11 Jan 2012 21:04:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Generating-binary-variables/m-p/28741#M6717</guid>
      <dc:creator>LarryWorley</dc:creator>
      <dc:date>2012-01-11T21:04:11Z</dc:date>
    </item>
    <item>
      <title>Generating binary variables</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Generating-binary-variables/m-p/28742#M6718</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Or, if you need to randomly assign which b variable gets a one, you could use something like:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data want (drop=i pop);&lt;/P&gt;&lt;P&gt;&amp;nbsp; array b(5);&lt;/P&gt;&lt;P&gt;&amp;nbsp; do pop=1 to 1000;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; do i=1 to 5;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; b(i)=0;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; b(int(5*ranuni(0))+1)=1;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; output;&lt;/P&gt;&lt;P&gt;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 11 Jan 2012 21:11:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Generating-binary-variables/m-p/28742#M6718</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2012-01-11T21:11:37Z</dc:date>
    </item>
    <item>
      <title>Generating binary variables</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Generating-binary-variables/m-p/28743#M6719</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt; I have problem in generating x with values 1 to 5. how can I generate it?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 11 Jan 2012 21:32:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Generating-binary-variables/m-p/28743#M6719</guid>
      <dc:creator>heba2000</dc:creator>
      <dc:date>2012-01-11T21:32:45Z</dc:date>
    </item>
    <item>
      <title>Generating binary variables</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Generating-binary-variables/m-p/28744#M6720</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You have to explain what you mean.&amp;nbsp; &lt;SPAN style="background-color: #ffffff;"&gt;int(5*ranuni(0))+1&amp;nbsp; would generate a number between 1 and 5.&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 11 Jan 2012 21:42:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Generating-binary-variables/m-p/28744#M6720</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2012-01-11T21:42:02Z</dc:date>
    </item>
    <item>
      <title>Generating binary variables</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Generating-binary-variables/m-p/28745#M6721</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt; I mean I wrote&lt;/P&gt;&lt;P&gt;proc iml;&lt;/P&gt;&lt;P&gt;x=j(60,5,.);&lt;/P&gt;&lt;P&gt;x=int(5*ranuni(0))+1;&lt;/P&gt;&lt;P&gt;but only one observation is generated and I need 60 observations with 5 variables&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 11 Jan 2012 22:03:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Generating-binary-variables/m-p/28745#M6721</guid>
      <dc:creator>heba2000</dc:creator>
      <dc:date>2012-01-11T22:03:50Z</dc:date>
    </item>
    <item>
      <title>Generating binary variables</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Generating-binary-variables/m-p/28746#M6722</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I'm not familiar enough with proc iml to help you.&amp;nbsp; In the datastep I showed, above, changing the value of &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &lt;SPAN style="background-color: #ffffff;"&gt;do pop=1 to 1000;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="background-color: #ffffff;"&gt;to&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="background-color: #ffffff;"&gt;&amp;nbsp; do pop=1 to 60;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="background-color: #ffffff;"&gt;would give you 60 records with 5 variables.&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 11 Jan 2012 22:08:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Generating-binary-variables/m-p/28746#M6722</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2012-01-11T22:08:00Z</dc:date>
    </item>
    <item>
      <title>Re: Generating binary variables</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Generating-binary-variables/m-p/28747#M6723</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;hi ... not IML, just a couple data steps ...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: 'courier new', courier;"&gt;* 60 observations, x ranges from 1 to 5;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: 'courier new', courier;"&gt;data x;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: 'courier new', courier;"&gt;do _n_ = 1 to 60;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: 'courier new', courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; x = ceil(5*ranuni(0));&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: 'courier new', courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;output;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: 'courier new', courier;"&gt;end;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: 'courier new', courier;"&gt;run;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: 'courier new', courier;"&gt;* add binary variables based on value of x;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: 'courier new', courier;"&gt;data x;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: 'courier new', courier;"&gt;array b(5) (5*0);&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: 'courier new', courier;"&gt;set x;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: 'courier new', courier;"&gt;b(x) = 1;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: 'courier new', courier;"&gt;output;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: 'courier new', courier;"&gt;b(x) = 0;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: 'courier new', courier;"&gt;run;&lt;/STRONG&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 11 Jan 2012 22:09:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Generating-binary-variables/m-p/28747#M6723</guid>
      <dc:creator>MikeZdeb</dc:creator>
      <dc:date>2012-01-11T22:09:41Z</dc:date>
    </item>
    <item>
      <title>Generating binary variables</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Generating-binary-variables/m-p/28748#M6724</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I am not really familiar with data generation and when I just copied your code it was accepted but the data was not showed to me it is clear that i am missing one command to get the data produced.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 11 Jan 2012 22:32:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Generating-binary-variables/m-p/28748#M6724</guid>
      <dc:creator>heba2000</dc:creator>
      <dc:date>2012-01-11T22:32:33Z</dc:date>
    </item>
    <item>
      <title>Generating binary variables</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Generating-binary-variables/m-p/28749#M6725</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;From both Mike's and my code, the data files would be shown in your work directory.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 11 Jan 2012 22:33:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Generating-binary-variables/m-p/28749#M6725</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2012-01-11T22:33:58Z</dc:date>
    </item>
    <item>
      <title>Generating binary variables</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Generating-binary-variables/m-p/28750#M6726</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt; I got the data &lt;/P&gt;&lt;P&gt;Thank you all for your help&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 11 Jan 2012 23:00:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Generating-binary-variables/m-p/28750#M6726</guid>
      <dc:creator>heba2000</dc:creator>
      <dc:date>2012-01-11T23:00:59Z</dc:date>
    </item>
  </channel>
</rss>

