<?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: Count number of negative and positive within a window in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Count-number-of-negative-and-positive-within-a-window/m-p/860641#M339980</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*
Assuming there is no gap between two date.
*/
data have;
input date id value;
datalines;
1 1 1
2 1 0
3 1 -1
4 1 0
5 1 -1
6 1 -1
1 2 0
2 2 1
3 2 0
;

%let n=3;
data want;
if _n_=1 then do;
  declare hash h();
  h.definekey('date');
  h.definedata('value');
  h.definedone();
end;
set have;
by id;
if first.id then h.clear();
h.add();
_value=value;
count_negative=0;
count_positive=0;
do _date=date-1 to date-&amp;amp;n. by -1;
  if h.find(key:_date)=0 then do;
    if value=-1 then count_negative+1;
 if value= 1 then count_positive+1;
  end;
end; 
value=_value;
drop _value _date;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 24 Feb 2023 12:04:55 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2023-02-24T12:04:55Z</dc:date>
    <item>
      <title>Count number of negative and positive within a window</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-number-of-negative-and-positive-within-a-window/m-p/860602#M339967</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;My code below works but clearly if I the lag value is 90 Or when I want to change the lag value, it will cause a lot of trouble.&lt;/P&gt;
&lt;P&gt;The idea is look back N records and count the number of -1 and 1.&lt;/P&gt;
&lt;P&gt;Can you please help to write a better code?&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;HHC&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input date id value;
datalines;
1 1 1
2 1 0
3 1 -1
4 1 0
5 1 -1
6 1 -1
1 2 0
2 2 1
3 2 0
;run;


data want; set have;
drop l1-l3 i;
l1=lag1(value);
l2=lag2(value);
l3=lag3(value);

array l(*) 	l1 l2 l3 ;
count_negative=0;
count_positive=0;
do i=1 to dim(l);
	if l{i}=-1 then count_negative=count_negative+1;
	else
	if l{i}=1 then count_positive=count_positive+1;

end;

run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 24 Feb 2023 03:47:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-number-of-negative-and-positive-within-a-window/m-p/860602#M339967</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2023-02-24T03:47:58Z</dc:date>
    </item>
    <item>
      <title>Re: Count number of negative and positive within a window</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-number-of-negative-and-positive-within-a-window/m-p/860607#M339968</link>
      <description>&lt;P&gt;Use a temporary array:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input date id value;
datalines;
1 1 1
2 1 0
3 1 -1
4 1 0
5 1 -1
6 1 -1
1 2 0
2 2 1
3 2 0
;

%let window = 3;

data want;
set have;
by id;
array win {&amp;amp;window.} _temporary_;
if first.id
then do i = 1 to &amp;amp;window.;
  win{i} = .;
end;
win{mod(_n_,&amp;amp;window.) + 1} = value;
count_negative = 0;
count_positive = 0;
do i = 1 to &amp;amp;window.;
  if win{i} = -1 then count_negative + 1;
  else if win{i} = 1 then count_positive + 1;
end;
drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The macro variable determines how far you want to look back.&lt;/P&gt;</description>
      <pubDate>Fri, 24 Feb 2023 06:05:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-number-of-negative-and-positive-within-a-window/m-p/860607#M339968</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-02-24T06:05:39Z</dc:date>
    </item>
    <item>
      <title>Re: Count number of negative and positive within a window</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-number-of-negative-and-positive-within-a-window/m-p/860641#M339980</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*
Assuming there is no gap between two date.
*/
data have;
input date id value;
datalines;
1 1 1
2 1 0
3 1 -1
4 1 0
5 1 -1
6 1 -1
1 2 0
2 2 1
3 2 0
;

%let n=3;
data want;
if _n_=1 then do;
  declare hash h();
  h.definekey('date');
  h.definedata('value');
  h.definedone();
end;
set have;
by id;
if first.id then h.clear();
h.add();
_value=value;
count_negative=0;
count_positive=0;
do _date=date-1 to date-&amp;amp;n. by -1;
  if h.find(key:_date)=0 then do;
    if value=-1 then count_negative+1;
 if value= 1 then count_positive+1;
  end;
end; 
value=_value;
drop _value _date;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 24 Feb 2023 12:04:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-number-of-negative-and-positive-within-a-window/m-p/860641#M339980</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2023-02-24T12:04:55Z</dc:date>
    </item>
    <item>
      <title>Re: Count number of negative and positive within a window</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-number-of-negative-and-positive-within-a-window/m-p/860754#M340017</link>
      <description>&lt;P&gt;Thank you all for helping.&lt;/P&gt;
&lt;P&gt;HHC&lt;/P&gt;</description>
      <pubDate>Fri, 24 Feb 2023 19:54:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-number-of-negative-and-positive-within-a-window/m-p/860754#M340017</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2023-02-24T19:54:56Z</dc:date>
    </item>
  </channel>
</rss>

