<?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: If-Then is not working in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/If-Then-is-not-working/m-p/770898#M244549</link>
    <description>&lt;P&gt;If a data step dos not have an explicit OUTPUT statements then SAS will automatically OUTPUT (write the current values to the saved dataset) at the end of the step.&lt;/P&gt;
&lt;P&gt;But your code has an OUTPUT statement so there is no extra output added.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want the value of GROUP to depend on the value of X you have to calculate GROUP before you write the data to the file.&lt;/P&gt;</description>
    <pubDate>Tue, 28 Sep 2021 14:03:12 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2021-09-28T14:03:12Z</dc:date>
    <item>
      <title>If-Then is not working</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-Then-is-not-working/m-p/770859#M244533</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to create a random number&amp;nbsp; and then assign it to a group according to its value.&lt;/P&gt;&lt;P&gt;I used very simple if-then, but it did not work!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;this is my code:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ranpatients;
seed=6789;
do i=1 to 40;
x1=int(ranuni(seed)*10000);
x=put(x1,z4.);
output;
end;
if x1 gt 5000 then group = "A"; else group = "B";
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;When I run it, I have empty group column.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="s4"&gt;&lt;SPAN class="s4"&gt;Any explanation ?&amp;nbsp;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="s4"&gt;&lt;SPAN class="s4"&gt;&lt;SPAN class="s4"&gt;&lt;SPAN class="s4"&gt;Thanks,&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 28 Sep 2021 12:04:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-Then-is-not-working/m-p/770859#M244533</guid>
      <dc:creator>lansoprazole</dc:creator>
      <dc:date>2021-09-28T12:04:59Z</dc:date>
    </item>
    <item>
      <title>Re: If-Then is not working</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-Then-is-not-working/m-p/770861#M244534</link>
      <description>&lt;P&gt;The IF statement has to go before the OUTPUT statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Otherwise (the way you have it), the record is output to the data set RANPATIENTS before GROUP is assigned a value, so GROUP is always missing.&lt;/P&gt;</description>
      <pubDate>Tue, 28 Sep 2021 12:15:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-Then-is-not-working/m-p/770861#M244534</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-09-28T12:15:21Z</dc:date>
    </item>
    <item>
      <title>Re: If-Then is not working</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-Then-is-not-working/m-p/770866#M244535</link>
      <description>&lt;P&gt;The order of statements is wrong.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You have implemented an algorithm that is similar to:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Write letter.&lt;/LI&gt;
&lt;LI&gt;Mail letter.&lt;/LI&gt;
&lt;LI&gt;Apply stamp to letter.&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;It is hard to put the stamp on the letter after it is already in mailbox.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ranpatients;
  seed=6789;
  do i=1 to 40;
    x1=int(ranuni(seed)*10000);
    x=put(x1,z4.);
    if x1 gt 5000 then group = "A"; else group = "B";
    output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 28 Sep 2021 12:38:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-Then-is-not-working/m-p/770866#M244535</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-09-28T12:38:27Z</dc:date>
    </item>
    <item>
      <title>Re: If-Then is not working</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-Then-is-not-working/m-p/770869#M244537</link>
      <description>&lt;P&gt;Apart from moving the IF, you should also consider using the more modern (and recommended) RAND function:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ranpatients;
call streaminit(6789);
do i = 1 to 40;
  x1 = rand("integer",1,9999);
  if x1 gt 5000 then group = "A"; else group = "B";
  x = put(x1,z4.);
  output;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 28 Sep 2021 12:46:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-Then-is-not-working/m-p/770869#M244537</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-09-28T12:46:19Z</dc:date>
    </item>
    <item>
      <title>Re: If-Then is not working</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-Then-is-not-working/m-p/770895#M244546</link>
      <description>Thanks.&lt;BR /&gt;&lt;BR /&gt;I tried it but the first observation (raw) was not assigned any group.&lt;BR /&gt;&lt;BR /&gt;I will try it again.</description>
      <pubDate>Tue, 28 Sep 2021 13:57:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-Then-is-not-working/m-p/770895#M244546</guid>
      <dc:creator>lansoprazole</dc:creator>
      <dc:date>2021-09-28T13:57:05Z</dc:date>
    </item>
    <item>
      <title>Re: If-Then is not working</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-Then-is-not-working/m-p/770896#M244547</link>
      <description>Thanks.&lt;BR /&gt;so output should be always at the end? or because I am using a variable from the loop?</description>
      <pubDate>Tue, 28 Sep 2021 13:58:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-Then-is-not-working/m-p/770896#M244547</guid>
      <dc:creator>lansoprazole</dc:creator>
      <dc:date>2021-09-28T13:58:37Z</dc:date>
    </item>
    <item>
      <title>Re: If-Then is not working</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-Then-is-not-working/m-p/770897#M244548</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/339703"&gt;@lansoprazole&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Thanks.&lt;BR /&gt;&lt;BR /&gt;I tried it but the first observation (raw) was not assigned any group.&lt;BR /&gt;&lt;BR /&gt;I will try it again.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I tried it too, and group is assigned to every observation in the data set. If it is not working for you, SHOW US the code.&lt;/P&gt;</description>
      <pubDate>Tue, 28 Sep 2021 14:02:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-Then-is-not-working/m-p/770897#M244548</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-09-28T14:02:05Z</dc:date>
    </item>
    <item>
      <title>Re: If-Then is not working</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-Then-is-not-working/m-p/770898#M244549</link>
      <description>&lt;P&gt;If a data step dos not have an explicit OUTPUT statements then SAS will automatically OUTPUT (write the current values to the saved dataset) at the end of the step.&lt;/P&gt;
&lt;P&gt;But your code has an OUTPUT statement so there is no extra output added.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want the value of GROUP to depend on the value of X you have to calculate GROUP before you write the data to the file.&lt;/P&gt;</description>
      <pubDate>Tue, 28 Sep 2021 14:03:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-Then-is-not-working/m-p/770898#M244549</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-09-28T14:03:12Z</dc:date>
    </item>
    <item>
      <title>Re: If-Then is not working</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-Then-is-not-working/m-p/770899#M244550</link>
      <description>It works now. I had to restart SAS</description>
      <pubDate>Tue, 28 Sep 2021 14:03:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-Then-is-not-working/m-p/770899#M244550</guid>
      <dc:creator>lansoprazole</dc:creator>
      <dc:date>2021-09-28T14:03:59Z</dc:date>
    </item>
    <item>
      <title>Re: If-Then is not working</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-Then-is-not-working/m-p/770900#M244551</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/339703"&gt;@lansoprazole&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Thanks.&lt;BR /&gt;so output should be always at the end? or because I am using a variable from the loop?&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I don't know if it should ALWAYS be at the end, as there may be examples where OUTPUT is not at the end.&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;What you are being advised to do is assign the value of GROUP before the OUTPUT.&lt;/P&gt;</description>
      <pubDate>Tue, 28 Sep 2021 14:04:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-Then-is-not-working/m-p/770900#M244551</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-09-28T14:04:12Z</dc:date>
    </item>
    <item>
      <title>Re: If-Then is not working</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-Then-is-not-working/m-p/770906#M244552</link>
      <description>Hi.&lt;BR /&gt;&lt;BR /&gt;I see you are using the RANUNI function.  Take a look at the RAND function that replaces EVERY previous random function.  You set the seed with "Call Streaminit(seed);" and to get uniform random numbers, you use "Variable = rand('Uniform');".  The beauty of this function is that by changing the first argument you can generate normal, Bernoulli, Poisson, etc.  You can also set a mean and standard deviation for normal distributions.  Take a look.  I think you will like this function.  SAS recommends it and says it generates "better" random strings.</description>
      <pubDate>Tue, 28 Sep 2021 14:31:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-Then-is-not-working/m-p/770906#M244552</guid>
      <dc:creator>Ron_Cody</dc:creator>
      <dc:date>2021-09-28T14:31:48Z</dc:date>
    </item>
  </channel>
</rss>

