<?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: calculation across observarions in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/calculation-across-observarions/m-p/593256#M170235</link>
    <description>&lt;P&gt;You can do it with a data step like this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want(drop=Y); 
	do until (last.id);
		set tbl;
		by id;
		if Y &amp;lt; 3000 then Ylower3000+1;
	end;
    output;
    Ylower3000=0;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 02 Oct 2019 06:17:54 GMT</pubDate>
    <dc:creator>PeterClemmensen</dc:creator>
    <dc:date>2019-10-02T06:17:54Z</dc:date>
    <item>
      <title>calculation across observarions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculation-across-observarions/m-p/593249#M170228</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;In the following data set there are 6 rows for each ID.&lt;/P&gt;
&lt;P&gt;I need to create a data set that contain 1 row for each customer and have information of #months that Y was lower than 3000.&lt;/P&gt;
&lt;P&gt;Expected results:&lt;/P&gt;
&lt;P&gt;for ID=1 we will get 0&lt;/P&gt;
&lt;P&gt;for ID=1 we will get 1&lt;/P&gt;
&lt;P&gt;for ID=1 we will get 4&lt;/P&gt;
&lt;P&gt;What is the way to do it please?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data tbl;
input ID Y;
cards;
1 3000
1 3400
1 3100
1 3200
1 3300
1 3300
2 3500
2 2900
2 3100
2 3100
2 3200
2 3300
3 3200
3 2900
3 2800
3 2800
3 2950
3 3200
;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 02 Oct 2019 04:24:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculation-across-observarions/m-p/593249#M170228</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2019-10-02T04:24:05Z</dc:date>
    </item>
    <item>
      <title>Re: calculation across observarions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculation-across-observarions/m-p/593250#M170229</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
   create table want as
   select ID, sum(Y&amp;lt;3000) as Ylower3000
   from tbl
   group by ID;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;ID  Ylower3000 
1   0 
2   1 
3   4 &lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 02 Oct 2019 06:08:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculation-across-observarions/m-p/593250#M170229</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-10-02T06:08:37Z</dc:date>
    </item>
    <item>
      <title>Re: calculation across observarions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculation-across-observarions/m-p/593255#M170234</link>
      <description>&lt;P&gt;What is the data set way to do it?&lt;/P&gt;
&lt;P&gt;I know to do it via proc sql&amp;nbsp; but don't know via data set way&lt;/P&gt;</description>
      <pubDate>Wed, 02 Oct 2019 06:12:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculation-across-observarions/m-p/593255#M170234</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2019-10-02T06:12:39Z</dc:date>
    </item>
    <item>
      <title>Re: calculation across observarions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculation-across-observarions/m-p/593256#M170235</link>
      <description>&lt;P&gt;You can do it with a data step like this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want(drop=Y); 
	do until (last.id);
		set tbl;
		by id;
		if Y &amp;lt; 3000 then Ylower3000+1;
	end;
    output;
    Ylower3000=0;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 02 Oct 2019 06:17:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculation-across-observarions/m-p/593256#M170235</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-10-02T06:17:54Z</dc:date>
    </item>
    <item>
      <title>Re: calculation across observarions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculation-across-observarions/m-p/593258#M170236</link>
      <description>&lt;P&gt;Of course, this approach relies on the input data to be sorted by ID. If it is not, then you can do something like this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data tbl;
input ID Y;
cards;
1 3000
1 3200
1 3300
1 3300
3 2800
3 2800
3 2950
3 3200
2 3500
2 2900
2 3100
2 3100
2 3200
1 3400
1 3100
2 3300
3 3200
3 2900
;
run;

data _null_;
    if _N_=1 then do;
        declare hash h(ordered:'Y');
        h.definekey('ID');
        h.definedata('ID', 'Ylower3000');
        h.definedone();
    end;

    set tbl end=lr;

    if h.check() ne 0 then do;
        Ylower3000=0;
        h.add();
    end;
    if Y&amp;lt;3000 then do;
        rc=h.find();
        Ylower3000+1;
        rc=h.replace();
    end;
    
    if lr then h.output(dataset:'want');
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 02 Oct 2019 06:31:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculation-across-observarions/m-p/593258#M170236</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-10-02T06:31:42Z</dc:date>
    </item>
    <item>
      <title>Re: calculation across observarions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculation-across-observarions/m-p/593713#M170442</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Just for fun. I think that direct addressing also will do in this case &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Bart&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data tbl;
input ID Y;
cards;
1 3000
1 3200
1 3300
1 3300
3 2800
3 2800
3 2950
3 3200
2 3500
2 2900
2 3100
2 3100
2 3200
1 3400
1 3100
2 3300
3 3200
3 2900
5 3200
5 2900
;
run;


%let size = 10000;
data want;
    array t[&amp;amp;size.] _temporary_; 

    min = &amp;amp;size.; max = 0;
    do until(eof);
      set tbl end=eof;
      t[id] + (Y&amp;lt;3000);
      min = min&amp;gt;&amp;lt;id;
      max = max&amp;lt;&amp;gt;id;
    end;

    do id = min to max;
      Ylower3000 = t[id];
      put _all_;
      if Ylower3000 &amp;gt; .z then output;
    end;
  stop;
  keep id Ylower3000; 
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 03 Oct 2019 13:43:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculation-across-observarions/m-p/593713#M170442</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2019-10-03T13:43:24Z</dc:date>
    </item>
  </channel>
</rss>

