<?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: Filling the gaps 3 - different conditions for adjacent rows in SAS Data Management</title>
    <link>https://communities.sas.com/t5/SAS-Data-Management/Filling-the-gaps-3-different-conditions-for-adjacent-rows/m-p/196591#M4313</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks Arthur for your answer!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I know that it is very similar to my previous questions, its just that I have some trouble intuiting the code, but I am trying to figure that out&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks again!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 07 Mar 2015 16:04:48 GMT</pubDate>
    <dc:creator>ilikesas</dc:creator>
    <dc:date>2015-03-07T16:04:48Z</dc:date>
    <item>
      <title>Filling the gaps 3 - different conditions for adjacent rows</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Filling-the-gaps-3-different-conditions-for-adjacent-rows/m-p/196589#M4311</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This is the 3rd question that I am posting from the same series - its too much to put into one question so I decided to break into several questions.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Suppose I have the following table:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;TABLE border="1" class="jiveBorder" height="128" style="border: 1px solid #000000; width: 329px; height: 130px;"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TH style="text-align: center; background-color: #6690bc; color: #ffffff; padding: 2px;" valign="middle"&gt;&lt;STRONG&gt;company&lt;/STRONG&gt;&lt;/TH&gt;&lt;TH style="text-align: center; background-color: #6690bc; color: #ffffff; padding: 2px;" valign="middle"&gt;year&lt;/TH&gt;&lt;TH style="text-align: center; background-color: #6690bc; color: #ffffff; padding: 2px;" valign="middle"&gt;cond1&lt;/TH&gt;&lt;TH style="text-align: center; background-color: #6690bc; color: #ffffff; padding: 2px;" valign="middle"&gt;&lt;STRONG&gt;cond2&lt;/STRONG&gt;&lt;/TH&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="padding: 2px;"&gt;A&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;2006&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;1&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="padding: 2px;"&gt;A&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;2009&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;1&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="padding: 2px;"&gt;B&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;2005&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;3&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;4&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="padding: 2px;"&gt;B&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;2010&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;3&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;6&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="padding: 2px;"&gt;B&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;2012&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;3&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;6&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In this case if two adjacent years for the same company have the same conditions then leave as is.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If two adjacent years have different conditions, then the year after the first year will have the same conditions as the first year. In this example, for Bditional year 2006 should be created with conditions (3,4)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;So the new table should look like:&lt;/P&gt;&lt;TABLE border="1" class="jiveBorder" height="178" style="border: 1px solid rgb(0, 0, 0); width: 295px; height: 180px;"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TH style="text-align: center; background-color: #6690bc; color: #ffffff; padding: 2px;" valign="middle"&gt;&lt;STRONG&gt;company&lt;/STRONG&gt;&lt;/TH&gt;&lt;TH style="text-align: center; background-color: #6690bc; color: #ffffff; padding: 2px;" valign="middle"&gt;&lt;STRONG&gt;year&lt;/STRONG&gt;&lt;/TH&gt;&lt;TH style="text-align: center; background-color: #6690bc; color: #ffffff; padding: 2px;" valign="middle"&gt;&lt;STRONG&gt;cond1&lt;/STRONG&gt;&lt;/TH&gt;&lt;TH style="text-align: center; background-color: #6690bc; color: #ffffff; padding: 2px;" valign="middle"&gt;&lt;STRONG&gt;cond2&lt;/STRONG&gt;&lt;/TH&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="padding: 2px;"&gt;A&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;2006&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;1&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="padding: 2px;"&gt;A&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;2009&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;1&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="padding: 2px;"&gt;B&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;2005&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;3&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;4&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="padding: 2px;"&gt;B&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;2006&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;3&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;4&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="padding: 2px;"&gt;B&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;2010&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;3&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;6&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="padding: 2px;"&gt;B&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;2012&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;3&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;6&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 07 Mar 2015 01:03:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Filling-the-gaps-3-different-conditions-for-adjacent-rows/m-p/196589#M4311</guid>
      <dc:creator>ilikesas</dc:creator>
      <dc:date>2015-03-07T01:03:33Z</dc:date>
    </item>
    <item>
      <title>Re: Filling the gaps 3 - different conditions for adjacent rows</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Filling-the-gaps-3-different-conditions-for-adjacent-rows/m-p/196590#M4312</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Next time attempt to solve the problem BEFORE asking for advice. You should have been able to figure this out by now:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data have;&lt;/P&gt;&lt;P&gt;&amp;nbsp; infile cards dlm='09'x;&lt;/P&gt;&lt;P&gt;&amp;nbsp; input company $ year cond1 cond2;&lt;/P&gt;&lt;P&gt;&amp;nbsp; cards;&lt;/P&gt;&lt;P&gt;A 2006 1 2&lt;/P&gt;&lt;P&gt;A 2009 1 2&lt;/P&gt;&lt;P&gt;B 2005 3 4&lt;/P&gt;&lt;P&gt;B 2010 3 6&lt;/P&gt;&lt;P&gt;B 2012 3 6&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;&amp;nbsp; set have end=eof;&lt;/P&gt;&lt;P&gt;&amp;nbsp; by company cond1 cond2 notsorted;&lt;/P&gt;&lt;P&gt;&amp;nbsp; if last.cond2 and not last.company then do;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; set have(firstobs=2 keep=year rename=year=nextyear);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; output;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if year+1 ne nextyear then do;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; year=year+1;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; output;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;&amp;nbsp; else output;&lt;/P&gt;&lt;P&gt;&amp;nbsp; drop nextyear;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 07 Mar 2015 01:35:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Filling-the-gaps-3-different-conditions-for-adjacent-rows/m-p/196590#M4312</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2015-03-07T01:35:14Z</dc:date>
    </item>
    <item>
      <title>Re: Filling the gaps 3 - different conditions for adjacent rows</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Filling-the-gaps-3-different-conditions-for-adjacent-rows/m-p/196591#M4313</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks Arthur for your answer!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I know that it is very similar to my previous questions, its just that I have some trouble intuiting the code, but I am trying to figure that out&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks again!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 07 Mar 2015 16:04:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Filling-the-gaps-3-different-conditions-for-adjacent-rows/m-p/196591#M4313</guid>
      <dc:creator>ilikesas</dc:creator>
      <dc:date>2015-03-07T16:04:48Z</dc:date>
    </item>
    <item>
      <title>Re: Filling the gaps 3 - different conditions for adjacent rows</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Filling-the-gaps-3-different-conditions-for-adjacent-rows/m-p/196592#M4314</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Just think through the process. In the present case if the data are already sorted by company and year, and then set (in the data step) with a by statement (namely by company cond1 cond2 notsorted), then:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;last.company will only be equal to 1 when the data step gets to the last record for each company.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;last.cond2 will only be equal to 1 when the data step get to a record where it is either the last record for a company or where the next record for a company has a different cond1 or cond2&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;set have(firstobs=2 keep=year rename=year=nextyear); creates a variable called nextyear that shows the year shown on the next record.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thus, the code outputs all records and, if the current record isn't the last record for a company AND the next record has a different cond1 or cond2 AND if the next record's year is greater than year+1, then output a new record with year=year+1.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 07 Mar 2015 16:45:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Filling-the-gaps-3-different-conditions-for-adjacent-rows/m-p/196592#M4314</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2015-03-07T16:45:44Z</dc:date>
    </item>
  </channel>
</rss>

