<?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: retain with by statement in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/retain-with-by-statement/m-p/203382#M37899</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You have some of the right pieces in place, but there's still some work to do.&amp;nbsp; Here is one approach:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc sort data=have;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; by T_CLASS_NUM time_variable;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; length flag2 $ 2;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; do until (last.T_CLASS_NUM);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; set have;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; by T_CLASS_NUM;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if DECISIONTYPENEW='AP' then flag2='AP';&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else if DECISIONTYPENEW='DE' and flag2=' ' then flag2='DE';&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; if flag2=' ' then flag2=DECISIONTYPENEW;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; do until (last.T_CLASS_NUM);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; set have;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; by T_CLASS_NUM;&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; end;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The program goes through all records for a T_CLASS_NUM, to determine the value for FLAG2.&amp;nbsp; Then it goes through the same set of records over again, outputting them with the final FLAG2 value.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Good luck.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 11 Jun 2015 15:14:31 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2015-06-11T15:14:31Z</dc:date>
    <item>
      <title>retain with by statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/retain-with-by-statement/m-p/203380#M37897</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;t&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'Courier New'; background: white; color: black;"&gt;I believe that I should use a retain statement and a by statement i.e. by id&amp;nbsp; but I am not sure how to program up what I want and I am &lt;/SPAN&gt;&lt;SPAN style="font-family: 'Courier New'; background: white; color: black;"&gt;Wondering if you have any insight. &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'Courier New'; background: white; color: black;"&gt; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'Courier New'; background: white; color: black;"&gt;I want if any occurrence for a given id has a certain value which we will call AP then all records for a given id have the value of AP for this id&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'Courier New'; background: white; color: black;"&gt;Further if none of the records for a given id&amp;nbsp; have value A but at least one has value DE then all records for a given id have the value of DE for this id&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'Courier New'; background: white; color: black;"&gt; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'Courier New'; background: white; color: black;"&gt;Further if none of the records for a given id&amp;nbsp; have value AP or value DE then keep most recent ordered value by time&amp;nbsp; (in the worked example, the last value, I have access to a time variable in the real data to assist in the sorting process). &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data person;&lt;/P&gt;&lt;P&gt;&amp;nbsp; input T_CLASS_NUM&amp;nbsp; DECISIONTYPENEW $;&lt;/P&gt;&lt;P&gt;&amp;nbsp; datalines;&lt;/P&gt;&lt;P&gt; 1&amp;nbsp; DF&lt;/P&gt;&lt;P&gt;&amp;nbsp; 1 DF&lt;/P&gt;&lt;P&gt;&amp;nbsp; 1 AP&lt;/P&gt;&lt;P&gt;&amp;nbsp; 1 DF&lt;/P&gt;&lt;P&gt; 2&amp;nbsp;&amp;nbsp; DF&lt;/P&gt;&lt;P&gt;2 DE&lt;/P&gt;&lt;P&gt;2 AP&lt;/P&gt;&lt;P&gt;3 AP&lt;/P&gt;&lt;P&gt;4 DE&lt;BR /&gt;5 DF&lt;BR /&gt;5 DF&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;proc sort data=person; by T_CLASS_NUM; run;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; data person2;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; set person;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; by T_CLASS_NUM;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; retain flag2;&lt;/P&gt;&lt;P&gt;if&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; first.T_CLASS_NUM then flag2 = .;&lt;/P&gt;&lt;P&gt;if DECISIONTYPENEW = "AP" then flag2 = "AP";&lt;/P&gt;&lt;P&gt;else if DECISIONTYPENEW = "DE" then flag2 = "DE";&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'Courier New'; background: white; color: black;"&gt; &lt;/SPAN&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;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt; &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 11 Jun 2015 14:01:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/retain-with-by-statement/m-p/203380#M37897</guid>
      <dc:creator>goldenone</dc:creator>
      <dc:date>2015-06-11T14:01:08Z</dc:date>
    </item>
    <item>
      <title>Re: retain with by statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/retain-with-by-statement/m-p/203381#M37898</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Suggest/encourage self-initiated desk-checking -- using SAS debugging techniques below:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;- PUTLOG _ALL_;&amp;nbsp; statement in various locations of your DATA step.&lt;/P&gt;&lt;P&gt;- PROC FREQ to analyze value combinations after the DATA step.&lt;/P&gt;&lt;P&gt;- PROC CONTENTS to analyze SAS file variable attributes after DATA step.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Personal coding technique for setting initial data-value (such as flag2) is to assign a blank-constant, however a missing-value (period character) will achieve same as long as the SAS variable is declared (either explicitly or implicitly) as it is expected -- considering some SAS system-default assignments for SAS variable types and lengths (which you would notice with PROC CONTENTS).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Scott Barry&lt;/P&gt;&lt;P&gt;SBBWorks, Inc.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 11 Jun 2015 15:11:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/retain-with-by-statement/m-p/203381#M37898</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2015-06-11T15:11:39Z</dc:date>
    </item>
    <item>
      <title>Re: retain with by statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/retain-with-by-statement/m-p/203382#M37899</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You have some of the right pieces in place, but there's still some work to do.&amp;nbsp; Here is one approach:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc sort data=have;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; by T_CLASS_NUM time_variable;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; length flag2 $ 2;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; do until (last.T_CLASS_NUM);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; set have;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; by T_CLASS_NUM;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if DECISIONTYPENEW='AP' then flag2='AP';&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else if DECISIONTYPENEW='DE' and flag2=' ' then flag2='DE';&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; if flag2=' ' then flag2=DECISIONTYPENEW;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; do until (last.T_CLASS_NUM);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; set have;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; by T_CLASS_NUM;&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; end;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The program goes through all records for a T_CLASS_NUM, to determine the value for FLAG2.&amp;nbsp; Then it goes through the same set of records over again, outputting them with the final FLAG2 value.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Good luck.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 11 Jun 2015 15:14:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/retain-with-by-statement/m-p/203382#M37899</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2015-06-11T15:14:31Z</dc:date>
    </item>
  </channel>
</rss>

