<?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 How to write an if/else statement based on a conditional event in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-write-an-if-else-statement-based-on-a-conditional-event/m-p/679808#M205317</link>
    <description>&lt;P&gt;Hi everyone,&lt;/P&gt;
&lt;P&gt;I'm having difficulty coming up with the correct logic coding to set a conditional if/else statement that is referencing a range of values as its reference. I'll provide a dataline with desired output below. The field district is my grouping value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;By district, I would like the following 3 unique values to populate the want column&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. If Have = 99 for all five years then Want = 1&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2. If Have = . before it equaled 99, then Want = 2 ;&lt;/P&gt;
&lt;P&gt;3. If Have = . after Have = 99, then Want = 3 ;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's the pre/post logic that i'm struggling with as there are instances where the event occurred after the start date, but did not continued until the end date. Thanks everyone! I appreciate the help, nate&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data Example;&lt;BR /&gt;infile datalines ;&lt;BR /&gt;input district year have want;&lt;BR /&gt;datalines;&lt;/P&gt;
&lt;P&gt;1 2006 99 1&lt;BR /&gt;1 2007 99 1&lt;BR /&gt;1 2008 99 1&lt;BR /&gt;1 2009 99 1&lt;BR /&gt;1 2010 99 1&lt;BR /&gt;2 2006 . 2&lt;BR /&gt;2 2007 . 2&lt;BR /&gt;2 2008 99 1&lt;BR /&gt;2 2009 99 1&lt;BR /&gt;2 2010 . 3&lt;BR /&gt;3 2006 99 1&lt;BR /&gt;3 2007 99 1&lt;BR /&gt;3 2008 99 1&lt;BR /&gt;3 2009 99 1&lt;BR /&gt;3 2010 . 3&lt;BR /&gt;4 2006 . 2&lt;BR /&gt;4 2007 . 2&lt;BR /&gt;4 2008 99 1&lt;BR /&gt;4 2009 99 1&lt;BR /&gt;4 2010 99 1&lt;BR /&gt;;&lt;/P&gt;</description>
    <pubDate>Thu, 27 Aug 2020 17:06:00 GMT</pubDate>
    <dc:creator>N8</dc:creator>
    <dc:date>2020-08-27T17:06:00Z</dc:date>
    <item>
      <title>How to write an if/else statement based on a conditional event</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-write-an-if-else-statement-based-on-a-conditional-event/m-p/679808#M205317</link>
      <description>&lt;P&gt;Hi everyone,&lt;/P&gt;
&lt;P&gt;I'm having difficulty coming up with the correct logic coding to set a conditional if/else statement that is referencing a range of values as its reference. I'll provide a dataline with desired output below. The field district is my grouping value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;By district, I would like the following 3 unique values to populate the want column&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. If Have = 99 for all five years then Want = 1&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2. If Have = . before it equaled 99, then Want = 2 ;&lt;/P&gt;
&lt;P&gt;3. If Have = . after Have = 99, then Want = 3 ;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's the pre/post logic that i'm struggling with as there are instances where the event occurred after the start date, but did not continued until the end date. Thanks everyone! I appreciate the help, nate&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data Example;&lt;BR /&gt;infile datalines ;&lt;BR /&gt;input district year have want;&lt;BR /&gt;datalines;&lt;/P&gt;
&lt;P&gt;1 2006 99 1&lt;BR /&gt;1 2007 99 1&lt;BR /&gt;1 2008 99 1&lt;BR /&gt;1 2009 99 1&lt;BR /&gt;1 2010 99 1&lt;BR /&gt;2 2006 . 2&lt;BR /&gt;2 2007 . 2&lt;BR /&gt;2 2008 99 1&lt;BR /&gt;2 2009 99 1&lt;BR /&gt;2 2010 . 3&lt;BR /&gt;3 2006 99 1&lt;BR /&gt;3 2007 99 1&lt;BR /&gt;3 2008 99 1&lt;BR /&gt;3 2009 99 1&lt;BR /&gt;3 2010 . 3&lt;BR /&gt;4 2006 . 2&lt;BR /&gt;4 2007 . 2&lt;BR /&gt;4 2008 99 1&lt;BR /&gt;4 2009 99 1&lt;BR /&gt;4 2010 99 1&lt;BR /&gt;;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Aug 2020 17:06:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-write-an-if-else-statement-based-on-a-conditional-event/m-p/679808#M205317</guid>
      <dc:creator>N8</dc:creator>
      <dc:date>2020-08-27T17:06:00Z</dc:date>
    </item>
    <item>
      <title>Re: How to write an if/else statement based on a conditional event</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-write-an-if-else-statement-based-on-a-conditional-event/m-p/679817#M205321</link>
      <description>&lt;P&gt;What if have is a non-missing value other than 99?&lt;/P&gt;</description>
      <pubDate>Thu, 27 Aug 2020 17:19:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-write-an-if-else-statement-based-on-a-conditional-event/m-p/679817#M205321</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-08-27T17:19:07Z</dc:date>
    </item>
    <item>
      <title>Re: How to write an if/else statement based on a conditional event</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-write-an-if-else-statement-based-on-a-conditional-event/m-p/679818#M205322</link>
      <description>Hi Kurt - the Have column will always be populated with non-unique value. It could be alpha or numeric, but it will be the same except for years where the event did not occur</description>
      <pubDate>Thu, 27 Aug 2020 17:23:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-write-an-if-else-statement-based-on-a-conditional-event/m-p/679818#M205322</guid>
      <dc:creator>N8</dc:creator>
      <dc:date>2020-08-27T17:23:04Z</dc:date>
    </item>
    <item>
      <title>Re: How to write an if/else statement based on a conditional event</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-write-an-if-else-statement-based-on-a-conditional-event/m-p/679838#M205331</link>
      <description>&lt;P&gt;Your logic is not clear in your post. My understanding for your logic is:&lt;/P&gt;&lt;P&gt;1. if have=99 then want=1&lt;/P&gt;&lt;P&gt;2. if have is missing and there is have=99 after this missing&amp;nbsp;in each&amp;nbsp;district then want=2;&lt;/P&gt;&lt;P&gt;3. if have is missing and there is have=99 before this missing in each district then want=3;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If my understanding is correct, my codes might work. Here, variable want1 should be equal to your expected value as list in want.&lt;/P&gt;&lt;PRE&gt;proc sort data=Example;
	by district year;
run;

data want;
	set Example;
	by district year;
	retain accum 0;
	if first.district then accum = 0;
	if have = 99 then i = 1;
	accum = sum(accum,i);
	if accum=0 then want1 = 2;		/* Setup 2 for leading missing per district */
	else if i = 1 then want1 = 1;	/* Setup 1 for have=99 per district */
	else if i = . then want1 =3;	/* Setup 3 for tailing missing per district */
run;&lt;/PRE&gt;</description>
      <pubDate>Thu, 27 Aug 2020 17:49:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-write-an-if-else-statement-based-on-a-conditional-event/m-p/679838#M205331</guid>
      <dc:creator>MINX</dc:creator>
      <dc:date>2020-08-27T17:49:05Z</dc:date>
    </item>
    <item>
      <title>Re: How to write an if/else statement based on a conditional event</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-write-an-if-else-statement-based-on-a-conditional-event/m-p/679849#M205334</link>
      <description>Thanks Minx. I appreciate your help and for letting me know a different way to write the logic steps. Thanks again! Nate</description>
      <pubDate>Thu, 27 Aug 2020 17:56:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-write-an-if-else-statement-based-on-a-conditional-event/m-p/679849#M205334</guid>
      <dc:creator>N8</dc:creator>
      <dc:date>2020-08-27T17:56:31Z</dc:date>
    </item>
  </channel>
</rss>

