<?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 by statement in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/by-statement/m-p/800087#M314689</link>
    <description>&lt;P&gt;I come across&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data pd4;
set pb3;
by region product sub_product;
if first.product then do;
cum_active=0;
cum_default=0;
end;
cum_active+active;
cum_default+Default;
cum_active_percent = cum_active / total_active;
cum_default_percent=cum_Default / total_Default;
run;


&lt;/PRE&gt;
&lt;P&gt;Hi I read the doc and couldn't understand the significance of assigning first, last to 1,0 etc in the above programs. Could you kindly explain?&lt;/P&gt;
&lt;P&gt;Aso could you explain the statements&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
cum_active+active; 
cum_default+Default;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;are they assigning values to some variable? are they boolean? what are they doing?&lt;CODE class=" language-sas"&gt;
&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 04 Mar 2022 18:21:02 GMT</pubDate>
    <dc:creator>HeatherNewton</dc:creator>
    <dc:date>2022-03-04T18:21:02Z</dc:date>
    <item>
      <title>by statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/by-statement/m-p/800087#M314689</link>
      <description>&lt;P&gt;I come across&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data pd4;
set pb3;
by region product sub_product;
if first.product then do;
cum_active=0;
cum_default=0;
end;
cum_active+active;
cum_default+Default;
cum_active_percent = cum_active / total_active;
cum_default_percent=cum_Default / total_Default;
run;


&lt;/PRE&gt;
&lt;P&gt;Hi I read the doc and couldn't understand the significance of assigning first, last to 1,0 etc in the above programs. Could you kindly explain?&lt;/P&gt;
&lt;P&gt;Aso could you explain the statements&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
cum_active+active; 
cum_default+Default;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;are they assigning values to some variable? are they boolean? what are they doing?&lt;CODE class=" language-sas"&gt;
&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 04 Mar 2022 18:21:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/by-statement/m-p/800087#M314689</guid>
      <dc:creator>HeatherNewton</dc:creator>
      <dc:date>2022-03-04T18:21:02Z</dc:date>
    </item>
    <item>
      <title>Re: by statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/by-statement/m-p/800094#M314693</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if first.product then do;
  cum_active=0;
  cum_default=0;
end;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This is Initializing variables to be used for the sum statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;cum_active+active; 
cum_default+Default;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Thereafter, the total is calculated.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For more information on the sum statement,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsref/n1dfiqj146yi2cn1maeju9wo7ijs.htm" target="_blank" rel="noopener"&gt;https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsref/n1dfiqj146yi2cn1maeju9wo7ijs.htm&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First data step variables are described here&lt;BR /&gt;&lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lrcon/n01a08zkzy5igbn173zjz82zsi1s.htm" target="_blank" rel="noopener"&gt;https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lrcon/n01a08zkzy5igbn173zjz82zsi1s.htm&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, I have a few requests.&lt;BR /&gt;Please do not post the text mixed in with the code block as it makes it difficult to see.&lt;BR /&gt;Also, if necessary, please make questions that you have already posted resolved.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 04 Mar 2022 08:28:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/by-statement/m-p/800094#M314693</guid>
      <dc:creator>japelin</dc:creator>
      <dc:date>2022-03-04T08:28:50Z</dc:date>
    </item>
    <item>
      <title>Re: by statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/by-statement/m-p/800113#M314703</link>
      <description>&lt;P&gt;For information on using the FIRST and LAST indicator variables in a BY-group analysis, see &lt;A href="https://blogs.sas.com/content/iml/2018/02/26/how-to-use-first-variable-and-last-variable-in-a-by-group-analysis-in-sas.html" target="_self"&gt;"How to use FIRST.variable and LAST.variable in a BY-group analysis in SAS."&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 04 Mar 2022 12:07:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/by-statement/m-p/800113#M314703</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2022-03-04T12:07:02Z</dc:date>
    </item>
    <item>
      <title>Re: by statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/by-statement/m-p/800306#M314783</link>
      <description>&lt;P&gt;The &lt;EM&gt;&lt;STRONG&gt;first.&lt;/STRONG&gt;&lt;/EM&gt; and &lt;EM&gt;&lt;STRONG&gt;last.&lt;/STRONG&gt;&lt;/EM&gt; automatic variables generated via the BY statement are just indicators about the current observation related to the preceding and following observations.&amp;nbsp; &amp;nbsp;When &lt;EM&gt;&lt;STRONG&gt;first.x=1&lt;/STRONG&gt;&lt;/EM&gt;, it means the current obs has a different value for x than the previous obs - otherwise &lt;EM&gt;&lt;STRONG&gt;first.x=0.&lt;/STRONG&gt;&lt;/EM&gt;. Similarly the &lt;EM&gt;&lt;STRONG&gt;last.x&lt;/STRONG&gt;&lt;/EM&gt; means the current value of x differs from the next obs&amp;nbsp; (i.e. SAS does a look-ahead for you).&amp;nbsp; &amp;nbsp;Now, it is possible for a record to have both &lt;EM&gt;&lt;STRONG&gt;first.x=1&lt;/STRONG&gt;&lt;/EM&gt; and &lt;EM&gt;&lt;STRONG&gt;last.x=1&lt;/STRONG&gt;&lt;/EM&gt;&amp;nbsp;(the current obs is a singleton) or &lt;EM&gt;&lt;STRONG&gt;first.x=0&lt;/STRONG&gt;&lt;/EM&gt; and &lt;EM&gt;&lt;STRONG&gt;last.x=0&lt;/STRONG&gt;&lt;/EM&gt;&amp;nbsp;(current obs is in the middle of a stream of constant x values).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now when you have BY a b c d. there will be multiple &lt;EM&gt;&lt;STRONG&gt;first.&lt;/STRONG&gt;&lt;/EM&gt; and multiple &lt;EM&gt;&lt;STRONG&gt;last.&lt;/STRONG&gt;&lt;/EM&gt;&amp;nbsp;automatic variables.&amp;nbsp; The (slightly) non-intuitive behavior of these is that if, in the middle of a series of constant B values, you have a change in A, &lt;EM&gt;&lt;STRONG&gt;both first.A and first.B are set to 1,&lt;/STRONG&gt;&lt;/EM&gt;&amp;nbsp;i.e. whenever a &lt;EM&gt;&lt;STRONG&gt;first.&lt;/STRONG&gt;&lt;/EM&gt; variable becomes 1, all the &lt;EM&gt;&lt;STRONG&gt;first.'s&lt;/STRONG&gt;&lt;/EM&gt; for variables to its right also become 1.&amp;nbsp; &amp;nbsp;Same with a set of &lt;EM&gt;&lt;STRONG&gt;last.&lt;/STRONG&gt;&lt;/EM&gt; variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So let me revise my first statement: whenever &lt;EM&gt;&lt;STRONG&gt;first.x=1&lt;/STRONG&gt;&lt;/EM&gt;, it means that the current x differs from the preceding x &lt;EM&gt;&lt;STRONG&gt;or the current value of some earlier variable in a by list differs from its predecessor&lt;/STRONG&gt;&lt;/EM&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Editted addition: the above refers to use of by statement in a DATA step.&lt;/P&gt;</description>
      <pubDate>Sat, 05 Mar 2022 05:29:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/by-statement/m-p/800306#M314783</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-03-05T05:29:17Z</dc:date>
    </item>
  </channel>
</rss>

