<?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: calculating data within group of data in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/calculating-data-within-group-of-data/m-p/821364#M324274</link>
    <description>&lt;P&gt;First of all, get rid of the five (or so) syntax errors you managed to put into three lines of code.&lt;/P&gt;
&lt;P&gt;Hint: "is not equal to" is not a valid SAS comparison operator. See&amp;nbsp;&lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lepg/p1n8bsqqd03xppn17pgvjpjlbhhs.htm" target="_blank" rel="noopener"&gt;SAS Operators&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Next, never call the LAG function in a conditional branch, as that will prevent the FIFO queue from being filled in each data step iteration.&lt;/P&gt;
&lt;P&gt;Third, please provide usable example data in a data step with datalines and show the complete log of the step you ran (all code and messages).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 02 Jul 2022 11:59:30 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2022-07-02T11:59:30Z</dc:date>
    <item>
      <title>calculating data within group of data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculating-data-within-group-of-data/m-p/821362#M324272</link>
      <description>&lt;P&gt;Hello all,&lt;/P&gt;&lt;P&gt;I'm trying to write a code that will calculate the column's colores in yellow in the attached file.&lt;/P&gt;&lt;P&gt;I tryed the following SAS&amp;nbsp; code :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;if ID_number is not equal to&amp;nbsp;ID_number_lag&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;than sales_in_first_year_in_the_ job=salse&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;else&amp;nbsp;sales_in_first_year_in_the_ job=lag(sales_in_first_year_in_the_ job)&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But I didn't receive the expected results.&lt;/P&gt;&lt;P&gt;Does anyone have an idea for codes (for the two colums)?&lt;/P&gt;&lt;P&gt;Thanks in advance!&lt;/P&gt;&lt;P&gt;Lior&lt;/P&gt;</description>
      <pubDate>Sat, 02 Jul 2022 10:24:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculating-data-within-group-of-data/m-p/821362#M324272</guid>
      <dc:creator>lioradam</dc:creator>
      <dc:date>2022-07-02T10:24:09Z</dc:date>
    </item>
    <item>
      <title>Re: calculating data within group of data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculating-data-within-group-of-data/m-p/821363#M324273</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/133482"&gt;@lioradam&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One idea is to use a double &lt;A href="https://www.lexjansen.com/phuse/2009/tu/TU01.pdf" target="_blank" rel="noopener"&gt;DOW-loop&lt;/A&gt;:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
do _n_=1 by 1 until(last.id);
  set have;
  by id notsorted;
  if n_yrs=1 then sales_first=sales;
  if last.id then sales_last=sales;
end;
do _n_=1 to _n_;
  set have;
  output;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This assumes that the input dataset (HAVE) is grouped by ID and that the last observation within each BY group contains the SALES value for the last year in the job. (If needed, the code could be modified to determine the observation with the highest number of years in the job per ID based on variable N_YRS rather than relying on ascending sort order.) Variable SALES_LAST could also be set unconditionally.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the first loop the new variables are populated. The second loop writes them to dataset WANT,&amp;nbsp;together with the content of HAVE.&lt;/P&gt;</description>
      <pubDate>Sat, 02 Jul 2022 11:49:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculating-data-within-group-of-data/m-p/821363#M324273</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2022-07-02T11:49:37Z</dc:date>
    </item>
    <item>
      <title>Re: calculating data within group of data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculating-data-within-group-of-data/m-p/821364#M324274</link>
      <description>&lt;P&gt;First of all, get rid of the five (or so) syntax errors you managed to put into three lines of code.&lt;/P&gt;
&lt;P&gt;Hint: "is not equal to" is not a valid SAS comparison operator. See&amp;nbsp;&lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lepg/p1n8bsqqd03xppn17pgvjpjlbhhs.htm" target="_blank" rel="noopener"&gt;SAS Operators&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Next, never call the LAG function in a conditional branch, as that will prevent the FIFO queue from being filled in each data step iteration.&lt;/P&gt;
&lt;P&gt;Third, please provide usable example data in a data step with datalines and show the complete log of the step you ran (all code and messages).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 02 Jul 2022 11:59:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculating-data-within-group-of-data/m-p/821364#M324274</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-07-02T11:59:30Z</dc:date>
    </item>
    <item>
      <title>Re: calculating data within group of data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculating-data-within-group-of-data/m-p/821365#M324275</link>
      <description>&lt;P&gt;You don't say what you are trying to calculate. Sum by group? Cumulative sum by group? Mean by group? Std Deviation by group? Kurtosis?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 02 Jul 2022 12:35:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculating-data-within-group-of-data/m-p/821365#M324275</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-07-02T12:35:23Z</dc:date>
    </item>
    <item>
      <title>Re: calculating data within group of data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculating-data-within-group-of-data/m-p/821368#M324277</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;Thank you very much!&lt;/P&gt;&lt;P&gt;Its works perfectly.&lt;/P&gt;&lt;P&gt;Since this code is new to me,&amp;nbsp; I wonder if I can adjust the code in order to receive a column for sales in the second year from the start of the group and a column for sales in the second year from the &lt;U&gt;end&lt;/U&gt; of the group (instead of column for sales in the first year and in the last year).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Kind Regards,&lt;/P&gt;&lt;P&gt;Lior&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 02 Jul 2022 14:29:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculating-data-within-group-of-data/m-p/821368#M324277</guid>
      <dc:creator>lioradam</dc:creator>
      <dc:date>2022-07-02T14:29:57Z</dc:date>
    </item>
    <item>
      <title>Re: calculating data within group of data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculating-data-within-group-of-data/m-p/821371#M324280</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;Just the value of the sales in the second year.&lt;/P&gt;&lt;P&gt;This means that there will be a column called "sales in the second year", in which in the first four rows the value will be &lt;STRONG&gt;102&lt;/STRONG&gt;, in the next three rows the value will be &lt;STRONG&gt;40&lt;/STRONG&gt; and in the last six rows, the value will be &lt;STRONG&gt;1352&lt;/STRONG&gt;.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Lior&lt;/P&gt;</description>
      <pubDate>Sat, 02 Jul 2022 15:05:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculating-data-within-group-of-data/m-p/821371#M324280</guid>
      <dc:creator>lioradam</dc:creator>
      <dc:date>2022-07-02T15:05:11Z</dc:date>
    </item>
    <item>
      <title>Re: calculating data within group of data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculating-data-within-group-of-data/m-p/821372#M324281</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/133482"&gt;@lioradam&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I wonder if I can adjust the code in order to receive a column for sales in the second year from the start of the group and a column for sales in the second year from the &lt;U&gt;end&lt;/U&gt; of the group (instead of column for sales in the first year and in the last year).&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Yes, this is possible. To be more flexible in view of potential future requirements (sales in third year, etc.) you could write the SALES values (of one ID at a time) to a temporary array and then select what you need from the array &lt;EM&gt;between&lt;/EM&gt; the two DOW-loops:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want(drop=_m);
array s[99] _temporary_;
do _n_=1 by 1 until(last.id);
  set have;
  by id notsorted;
  s[n_yrs]=sales;
  _m=max(_m,n_yrs);
end;
sales_second=s[2];
if _m&amp;gt;=2 then sales_lastbut1=s[_m-1];
do _n_=1 to _n_;
  set have;
  output;
end;
call missing(of s[*]);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This code does no longer rely on the order of observations within the BY groups.&lt;/P&gt;</description>
      <pubDate>Sat, 02 Jul 2022 15:18:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculating-data-within-group-of-data/m-p/821372#M324281</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2022-07-02T15:18:11Z</dc:date>
    </item>
    <item>
      <title>Re: calculating data within group of data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculating-data-within-group-of-data/m-p/821377#M324285</link>
      <description>&lt;P&gt;Thank you!&lt;/P&gt;&lt;P&gt;Lior&lt;/P&gt;</description>
      <pubDate>Sat, 02 Jul 2022 16:15:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculating-data-within-group-of-data/m-p/821377#M324285</guid>
      <dc:creator>lioradam</dc:creator>
      <dc:date>2022-07-02T16:15:25Z</dc:date>
    </item>
  </channel>
</rss>

