<?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 in PROC SQL in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Retain-in-PROC-SQL/m-p/410866#M100408</link>
    <description>&lt;P&gt;In general you cannot since PROC SQL doesn't know anything about order.&lt;/P&gt;
&lt;P&gt;But your example seems to just be using RETAIN to generate a group level condition.&lt;/P&gt;
&lt;P&gt;It is easy to do this with binary variables since to test if any observation met the condition you can just take the MAX() over the whole group.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql ;
create table output as
  select by_va1
       , by_va2
       , max(col1 = 1 and col2 = 9) as flag1
       , max(col1 = 1 and col2 = 13) as flag2
       , max(col1 = 1 and col2 = 14) as flag3
  from sample
  group by by_va1, by_va2
;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you need to translate to ORACLE code you can replace the boolean expressions with equivalent CASE clause instead.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;       , max(case when (col1 = 1 and col2 = 9) then 1 else 0 end) as flag1&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 06 Nov 2017 14:43:47 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2017-11-06T14:43:47Z</dc:date>
    <item>
      <title>Retain in PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-in-PROC-SQL/m-p/410817#M100394</link>
      <description>&lt;P&gt;I am trying to convert the below BASE sas code into proc sql. Kindly let me know the best possible solution in PROC SQL to get the similar output.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data sample;&lt;BR /&gt;input by_va1 by_va2 col1 col2;&lt;BR /&gt;infile datalines;&lt;BR /&gt;datalines;&lt;BR /&gt;1 2 1 10&lt;BR /&gt;1 2 1 11&lt;BR /&gt;1 2 1 12&lt;BR /&gt;1 2 3 9&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data output;&lt;BR /&gt;set sample;&lt;BR /&gt;by by_va1 by_va2;&lt;BR /&gt;retain flag1 flag2 flag3 0;&lt;/P&gt;&lt;P&gt;if first.by_va1 then do;&lt;BR /&gt;flag1 = 0;&lt;BR /&gt;flag2 = 0;&lt;BR /&gt;flag3 = 0;&lt;BR /&gt;end;&lt;/P&gt;&lt;P&gt;if col1 = 1 and col2 = 9 then do;&lt;BR /&gt;flag1 = 1;&lt;BR /&gt;end;&lt;/P&gt;&lt;P&gt;if col1 = 1 and col2 = 13 then do;&lt;BR /&gt;flag2 = 1;&lt;BR /&gt;end;&lt;/P&gt;&lt;P&gt;if col1 = 1 and col2 = 14 then do;&lt;BR /&gt;flag3 = 1;&lt;BR /&gt;end;&lt;/P&gt;&lt;P&gt;if last.by_va1 then do;&lt;BR /&gt;output;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Mon, 06 Nov 2017 12:33:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-in-PROC-SQL/m-p/410817#M100394</guid>
      <dc:creator>LOVE_SAA</dc:creator>
      <dc:date>2017-11-06T12:33:44Z</dc:date>
    </item>
    <item>
      <title>Re: Retain in PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-in-PROC-SQL/m-p/410820#M100396</link>
      <description>&lt;P&gt;SQL does not have a concept of row order - in this respect is the exact opposite of datastep.&amp;nbsp; You cannot "retain" data down.&amp;nbsp; An example method would be.&amp;nbsp; And note your test data wouldn't trigger the flags, so I changed it.&lt;/P&gt;
&lt;PRE&gt;data sample;&lt;BR /&gt;input by_va1 by_va2 col1 col2;&lt;BR /&gt;infile datalines;&lt;BR /&gt;datalines;&lt;BR /&gt;1 2 1 9&lt;BR /&gt;1 2 1 11&lt;BR /&gt;1 2 1 13&lt;BR /&gt;1 2 3 9&lt;BR /&gt;;&lt;BR /&gt;run;&lt;BR /&gt;proc sql;&lt;BR /&gt; create table WANT as&lt;BR /&gt; select *,&lt;BR /&gt; case when sum(case when COL1=1 and COL2=9 then 1 else 0 end) &amp;gt; 0 then 1 else 0 end as FLAG1,&lt;BR /&gt; case when sum(case when COL1=1 and COL2=13 then 1 else 0 end) &amp;gt; 0 then 1 else 0 end as FLAG2,&lt;BR /&gt; case when sum(case when COL1=1 and COL2=14 then 1 else 0 end) &amp;gt; 0 then 1 else 0 end as FLAG3&lt;BR /&gt; from SAMPLE A;&lt;BR /&gt;quit;&lt;/PRE&gt;
&lt;P&gt;The question is why, if you have working code go through the whole validation, documentation process, just to do it in SQL?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 06 Nov 2017 12:55:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-in-PROC-SQL/m-p/410820#M100396</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-11-06T12:55:58Z</dc:date>
    </item>
    <item>
      <title>Re: Retain in PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-in-PROC-SQL/m-p/410823#M100398</link>
      <description>&lt;P&gt;Thanks for the perfect answer. I am trying to convert BASE SAS code into SAS DI and the PROC SQL code will be executed over the ORACLE server. Hence trying for the best possible way.&lt;/P&gt;</description>
      <pubDate>Mon, 06 Nov 2017 13:04:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-in-PROC-SQL/m-p/410823#M100398</guid>
      <dc:creator>LOVE_SAA</dc:creator>
      <dc:date>2017-11-06T13:04:18Z</dc:date>
    </item>
    <item>
      <title>Re: Retain in PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-in-PROC-SQL/m-p/410837#M100401</link>
      <description>&lt;P&gt;To achieve a single output row like the original data step, a group by is necessary in the SQL:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; create table WANT as
 select by_va1, by_va2,
 case when sum(case when COL1=1 and COL2=9 then 1 else 0 end) &amp;gt; 0 then 1 else 0 end as FLAG1,
 case when sum(case when COL1=1 and COL2=13 then 1 else 0 end) &amp;gt; 0 then 1 else 0 end as FLAG2,
 case when sum(case when COL1=1 and COL2=14 then 1 else 0 end) &amp;gt; 0 then 1 else 0 end as FLAG3
 from SAMPLE A
group by by_va1, by_va2;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 06 Nov 2017 13:42:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-in-PROC-SQL/m-p/410837#M100401</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-11-06T13:42:56Z</dc:date>
    </item>
    <item>
      <title>Re: Retain in PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-in-PROC-SQL/m-p/410866#M100408</link>
      <description>&lt;P&gt;In general you cannot since PROC SQL doesn't know anything about order.&lt;/P&gt;
&lt;P&gt;But your example seems to just be using RETAIN to generate a group level condition.&lt;/P&gt;
&lt;P&gt;It is easy to do this with binary variables since to test if any observation met the condition you can just take the MAX() over the whole group.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql ;
create table output as
  select by_va1
       , by_va2
       , max(col1 = 1 and col2 = 9) as flag1
       , max(col1 = 1 and col2 = 13) as flag2
       , max(col1 = 1 and col2 = 14) as flag3
  from sample
  group by by_va1, by_va2
;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you need to translate to ORACLE code you can replace the boolean expressions with equivalent CASE clause instead.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;       , max(case when (col1 = 1 and col2 = 9) then 1 else 0 end) as flag1&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 06 Nov 2017 14:43:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-in-PROC-SQL/m-p/410866#M100408</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-11-06T14:43:47Z</dc:date>
    </item>
  </channel>
</rss>

