<?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: How to replace a join with a datastep in SAS in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-replace-a-join-with-a-datastep-in-SAS/m-p/761460#M240966</link>
    <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/366382"&gt;@kashlik123&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The date sequences in your sample data have no gaps (of &amp;gt;=2 days) and no duplicates within an ID. My suggested DATA step below only assumes the latter. If there are also no gaps in your real data, the code can be simplified. (Maybe you could handle duplicate dates, if any, in a preliminary PROC SUMMARY step aggregating&amp;nbsp;&lt;FONT face="courier new,courier"&gt;var&lt;/FONT&gt; values by &lt;FONT face="courier new,courier"&gt;ID dt&lt;/FONT&gt;. But this depends on how you'd define the means in the presence of duplicate dates.)&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want(drop=_:);
array _3d[0:2] _temporary_;
array _6d[0:5] _temporary_;
set have_fin;
by id dt;
_d=dif(dt);
if first.id | _d&amp;gt;=6 then call missing(of _3d[*], of _6d[*], _i3, _i6);
else do;
  do _j=1 to _d-1;
    _i6+1;
    _6d[mod(_i6,6)]=.;
  end;
  if _d&amp;gt;=3 then call missing(of _3d[*], _i3);
  else do _j=1 to _d-1;
    _i3+1;
    _3d[mod(_i3,3)]=.;
  end;
end;
_i3+1;
_i6+1;
_3d[mod(_i3,3)]=var;
_6d[mod(_i6,6)]=var;
mean_var_3d=mean(of _3d[*]);
mean_var_6d=mean(of _6d[*]);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 13 Aug 2021 16:12:49 GMT</pubDate>
    <dc:creator>FreelanceReinh</dc:creator>
    <dc:date>2021-08-13T16:12:49Z</dc:date>
    <item>
      <title>How to replace a join with a datastep in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-replace-a-join-with-a-datastep-in-SAS/m-p/761395#M240932</link>
      <description>&lt;DIV class="votecell post-layout--left"&gt;&lt;DIV class="js-voting-container d-flex jc-center fd-column ai-stretch gs4 fc-black-200"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;&lt;DIV class="postcell post-layout--right"&gt;&lt;DIV class="s-prose js-post-body"&gt;&lt;P&gt;I am trying to calculate some statistics for a given variable based on the client's id and the time horizon. My current solution is show below, however, I would like to know if there is a way to reformat the code into a datastep instead of an sql join, because the join requires an excessive amount of time to execute on my real dataset.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have1(drop=t);
id = 1;
dt = '31dec2020'd;
do t=1 to 10;
    dt = dt + 1;
    var = rand('uniform');
    output;
end;
format dt ddmmyyp10.;
run;

data have2(drop=t);
id = 2;
dt = '31dec2020'd;
do t=1 to 10;
    dt = dt + 1;
    var = rand('uniform');
    output;
end;
format dt ddmmyyp10.;
run;

data have_fin;
set have1 have2;
run;

Proc sql;
create table want1 as
select a.id, a.dt,a.var, mean(b.var) as mean_var_3d
from have_fin as a
left join have_fin as b
    on a.id = b.id and intnx('day',a.dt,-3,'S') &amp;lt; b.dt &amp;lt;= a.dt
group by 1,2,3;
Quit;

Proc sql;
create table want2 as
select a.id, a.dt,a.var, mean(b.var) as mean_var_3d
from have_fin as a
left join have_fin as b
    on a.id = b.id and intnx('day',a.dt,-6,'S') &amp;lt; b.dt &amp;lt;= a.dt
group by 1,2,3;
Quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Fri, 13 Aug 2021 12:06:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-replace-a-join-with-a-datastep-in-SAS/m-p/761395#M240932</guid>
      <dc:creator>kashlik123</dc:creator>
      <dc:date>2021-08-13T12:06:05Z</dc:date>
    </item>
    <item>
      <title>Re: How to replace a join with a datastep in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-replace-a-join-with-a-datastep-in-SAS/m-p/761428#M240949</link>
      <description>&lt;P&gt;Is this supposed to be some sort of "rolling" mean where the number of records in the denominator keeps changing?&lt;/P&gt;
&lt;P&gt;If so then you might look into Proc Expand if you have SAS/ETS licensed.&lt;/P&gt;
&lt;P&gt;If not describe what this is a supposed to be doing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;BTW, you can really simplify your "example" data by creating one data set with another do loop:&lt;/P&gt;
&lt;PRE&gt;data have1(drop=t);
   do id = 1,2;/* list explicit values */
      /* or use Do id=1 to 2; or more if desired*/
      dt = '31dec2020'd;
      do t=1 to 10;
          dt = dt + 1;
          var = rand('uniform');
          output;
      end;
   end;
   format dt ddmmyyp10.;
run;&lt;/PRE&gt;</description>
      <pubDate>Fri, 13 Aug 2021 14:17:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-replace-a-join-with-a-datastep-in-SAS/m-p/761428#M240949</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-08-13T14:17:03Z</dc:date>
    </item>
    <item>
      <title>Re: How to replace a join with a datastep in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-replace-a-join-with-a-datastep-in-SAS/m-p/761460#M240966</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/366382"&gt;@kashlik123&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The date sequences in your sample data have no gaps (of &amp;gt;=2 days) and no duplicates within an ID. My suggested DATA step below only assumes the latter. If there are also no gaps in your real data, the code can be simplified. (Maybe you could handle duplicate dates, if any, in a preliminary PROC SUMMARY step aggregating&amp;nbsp;&lt;FONT face="courier new,courier"&gt;var&lt;/FONT&gt; values by &lt;FONT face="courier new,courier"&gt;ID dt&lt;/FONT&gt;. But this depends on how you'd define the means in the presence of duplicate dates.)&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want(drop=_:);
array _3d[0:2] _temporary_;
array _6d[0:5] _temporary_;
set have_fin;
by id dt;
_d=dif(dt);
if first.id | _d&amp;gt;=6 then call missing(of _3d[*], of _6d[*], _i3, _i6);
else do;
  do _j=1 to _d-1;
    _i6+1;
    _6d[mod(_i6,6)]=.;
  end;
  if _d&amp;gt;=3 then call missing(of _3d[*], _i3);
  else do _j=1 to _d-1;
    _i3+1;
    _3d[mod(_i3,3)]=.;
  end;
end;
_i3+1;
_i6+1;
_3d[mod(_i3,3)]=var;
_6d[mod(_i6,6)]=var;
mean_var_3d=mean(of _3d[*]);
mean_var_6d=mean(of _6d[*]);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 13 Aug 2021 16:12:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-replace-a-join-with-a-datastep-in-SAS/m-p/761460#M240966</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-08-13T16:12:49Z</dc:date>
    </item>
  </channel>
</rss>

