<?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 create an average of data in a datastep in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-an-average-of-data-in-a-datastep/m-p/475153#M122159</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data want;
 set d01;
 by usubjid;
 output;
 lag_chg=lag(chg);
 lag2_chg=lag2(chg);
 if last.usubjid then do;
   chg=mean(chg,lag_chg,lag2_chg);
   output;
 end;
 drop lag:;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 03 Jul 2018 12:23:21 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2018-07-03T12:23:21Z</dc:date>
    <item>
      <title>How to create an average of data in a datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-an-average-of-data-in-a-datastep/m-p/475114#M122153</link>
      <description>&lt;P&gt;Hi this is how my data looks and i want to include one more at bottom of dataset by taking an avg of last three records&lt;/P&gt;
&lt;P&gt;pls suggest&lt;/P&gt;
&lt;P&gt;DATA D01;&lt;BR /&gt;infile datalines missover;&lt;BR /&gt;param='weight(kg)';&lt;BR /&gt;INPUT USUBJID AVISIT $ AVISITN VISITNUM&lt;BR /&gt;VSSEQ @ 26 ABLFL $1. AVAL BASE CHG ;&lt;BR /&gt;&lt;BR /&gt;DATALINES;&lt;BR /&gt;101 screening -4 1 1164 99 100&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;BR /&gt;101 run-in -2 2 1165 101 100&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;BR /&gt;101 baseline 0 3 1166 y 100 100 0&lt;BR /&gt;101 week24 24 4 1167 94 100 -6&lt;BR /&gt;101 week48 24 5 1168 92 100 -8&lt;BR /&gt;101 week52 52 6 1169 95 100 -5&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Tue, 03 Jul 2018 09:33:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-an-average-of-data-in-a-datastep/m-p/475114#M122153</guid>
      <dc:creator>sas_mania</dc:creator>
      <dc:date>2018-07-03T09:33:22Z</dc:date>
    </item>
    <item>
      <title>Re: How to create an average of data in a datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-an-average-of-data-in-a-datastep/m-p/475116#M122154</link>
      <description>&lt;P&gt;Bit of a faff, but this works (note how I don't use uppercase coding and I use the code window - its the {i} above post area to retain indents and such like):&lt;/P&gt;
&lt;PRE&gt;data d01;
  infile datalines missover;
  param='weight(kg)';
  input usubjid avisit $ avisitn visitnum vsseq ablfl aval base chg;
datalines;
101 screening -4 1 1164 99 100 
101 run-in -2 2 1165 101 100 
101 baseline 0 3 1166 y 100 100 0
101 week24 24 4 1167 94 100 -6
101 week48 24 5 1168 92 100 -8
101 week52 52 6 1169 95 100 -5
;
run;

proc sort data=d01 out=tmp;
  by usubjid descending avisitn;
run;

data tmp (drop=i tot);
  set tmp;
  retain i tot;
  by usubjid;
  i=ifn(first.usubjid,1,i+1);
  if i &amp;lt;= 3 then tot=sum(tot,aval);
  if last.usubjid then do;
    avisit="Average";
    avisitn=.;
    aval=tot/3;
    output;
  end;
run;

data want;
  set d01 tmp;
run;&lt;/PRE&gt;
&lt;P&gt;Do note your output will always be 100 with that data.&lt;/P&gt;</description>
      <pubDate>Tue, 03 Jul 2018 09:46:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-an-average-of-data-in-a-datastep/m-p/475116#M122154</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-07-03T09:46:34Z</dc:date>
    </item>
    <item>
      <title>Re: How to create an average of data in a datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-an-average-of-data-in-a-datastep/m-p/475153#M122159</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data want;
 set d01;
 by usubjid;
 output;
 lag_chg=lag(chg);
 lag2_chg=lag2(chg);
 if last.usubjid then do;
   chg=mean(chg,lag_chg,lag2_chg);
   output;
 end;
 drop lag:;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 03 Jul 2018 12:23:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-an-average-of-data-in-a-datastep/m-p/475153#M122159</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-07-03T12:23:21Z</dc:date>
    </item>
  </channel>
</rss>

