<?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 to calculate across rows: Number of  times touch failure in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Retain-to-calculate-across-rows-Number-of-times-touch-failure/m-p/610922#M178003</link>
    <description>&lt;P&gt;Great and thank you.&lt;/P&gt;
&lt;P&gt;As I see&amp;nbsp; we can also use retain without set&amp;nbsp; initial value of 0.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 11 Dec 2019 08:51:45 GMT</pubDate>
    <dc:creator>Ronein</dc:creator>
    <dc:date>2019-12-11T08:51:45Z</dc:date>
    <item>
      <title>Retain to calculate across rows: Number of  times touch failure</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-to-calculate-across-rows-Number-of-times-touch-failure/m-p/610908#M177996</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;I have multiple rows for each customer.&lt;/P&gt;
&lt;P&gt;Task1- For each customer create binary indicator that get value 1 if the customer touch grade 11 and 0 otherwise&lt;/P&gt;
&lt;P&gt;Task2-For each customer create a new variable called "No_Failures"&amp;nbsp; &amp;nbsp;that count number of months that he/she touch grade 11&lt;/P&gt;
&lt;P&gt;I know to calculate it by proc sql.&lt;/P&gt;
&lt;P&gt;My question is how to calculate it using RETAIN&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data raw_tbl;
input ID YYMM  grade;
cards;
1 1901 2
1 1902 2
1 1903 3
1 1904 3
1 1905 2
1 1906 4
2 1901 9
2 1902 10
2 1903 10
2 1904 11
2 1905 11
2 1906 11
3 1901 10
3 1902 11
3 1903 11
3 1904 11
3 1905 11
3 1906 11
;
run;

 PROC SQL;
	create table required_a  as
	select ID,
           sum(case when grade=11 then 1 else 0 end ) as No_Failures,
           max(case when grade=11 then 1 else 0 end ) as Ind_Touch_Failure ,
/*		   case when calculated No_Failures&amp;gt;=1 then 1 else 0 end as Ind_Touch_Failure*/
	from  raw_tbl
	group by ID
;
QUIT;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 11 Dec 2019 07:20:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-to-calculate-across-rows-Number-of-times-touch-failure/m-p/610908#M177996</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2019-12-11T07:20:34Z</dc:date>
    </item>
    <item>
      <title>Re: Retain to calculate across rows: Number of  times touch failure</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-to-calculate-across-rows-Number-of-times-touch-failure/m-p/610911#M177998</link>
      <description>&lt;P&gt;This uses retain and gives the same output&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set raw_tbl;
   by ID;
   if first.id then do;
      No_Failures=0;
      Ind_Touch_Failure=0;
   end;
   if grade=11 then do;
      No_Failures=No_Failures+1;
      Ind_Touch_Failure=1;
   end;
   if last.id;
   retain No_Failures Ind_Touch_Failure 0;
   keep id No_Failures Ind_Touch_Failure;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 11 Dec 2019 07:34:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-to-calculate-across-rows-Number-of-times-touch-failure/m-p/610911#M177998</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-12-11T07:34:27Z</dc:date>
    </item>
    <item>
      <title>Re: Retain to calculate across rows: Number of  times touch failure</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-to-calculate-across-rows-Number-of-times-touch-failure/m-p/610912#M177999</link>
      <description>&lt;P&gt;This is simpler though.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   No_Failures=0;Ind_Touch_Failure=0;
   do until (last.id);
      set raw_tbl;
      by id;
      if grade=11 then do;
         No_Failures+1;
         Ind_Touch_Failure=1;
      end;
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 11 Dec 2019 07:36:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-to-calculate-across-rows-Number-of-times-touch-failure/m-p/610912#M177999</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-12-11T07:36:40Z</dc:date>
    </item>
    <item>
      <title>Re: Retain to calculate across rows: Number of  times touch failure</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-to-calculate-across-rows-Number-of-times-touch-failure/m-p/610922#M178003</link>
      <description>&lt;P&gt;Great and thank you.&lt;/P&gt;
&lt;P&gt;As I see&amp;nbsp; we can also use retain without set&amp;nbsp; initial value of 0.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 11 Dec 2019 08:51:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-to-calculate-across-rows-Number-of-times-touch-failure/m-p/610922#M178003</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2019-12-11T08:51:45Z</dc:date>
    </item>
    <item>
      <title>Re: Retain to calculate across rows: Number of  times touch failure</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-to-calculate-across-rows-Number-of-times-touch-failure/m-p/610924#M178005</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In addition to&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;&amp;nbsp;'s answer, you don't need to specify a RETAIN statement as the statement &lt;FONT color="#800080"&gt;No_Failures + 1&lt;/FONT&gt; already makes a cumulative sum (the RETAIN is "included", assuming the first value of the counter is 0).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In case you want to initialize your counter to a different number than 0, you can use the RETAIN statement to assign this initial value. But no need in your case.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 11 Dec 2019 08:53:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-to-calculate-across-rows-Number-of-times-touch-failure/m-p/610924#M178005</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2019-12-11T08:53:50Z</dc:date>
    </item>
    <item>
      <title>Re: Retain to calculate across rows: Number of  times touch failure</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-to-calculate-across-rows-Number-of-times-touch-failure/m-p/610925#M178006</link>
      <description>&lt;P&gt;please try&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data want;
set raw_tbl;
by id yymm;
retain Ind_Touch_Failure No_Failures;
if first.id then do; Ind_Touch_Failure=.;No_Failures=.;end;
if grade=11 then Ind_Touch_Failure=1;
else Ind_Touch_Failure=0;
if grade=11 then No_Failures+1;
else No_Failures=0;
if last.id;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 11 Dec 2019 08:54:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-to-calculate-across-rows-Number-of-times-touch-failure/m-p/610925#M178006</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2019-12-11T08:54:09Z</dc:date>
    </item>
    <item>
      <title>Re: Retain to calculate across rows: Number of  times touch failure</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-to-calculate-across-rows-Number-of-times-touch-failure/m-p/610928#M178008</link>
      <description>&lt;P&gt;Thank you so much&lt;/P&gt;</description>
      <pubDate>Wed, 11 Dec 2019 09:06:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-to-calculate-across-rows-Number-of-times-touch-failure/m-p/610928#M178008</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2019-12-11T09:06:21Z</dc:date>
    </item>
  </channel>
</rss>

