<?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: Obtaining mean for variable number of variables per subject in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Obtaining-mean-for-variable-number-of-variables-per-subject/m-p/305773#M65266</link>
    <description>&lt;P&gt;Transpose and use proc means.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=test;
by id disease time;
run;

proc transpose data=test out=long;
by id disease time;
var dm1-dm7;
run;

data long;
set long;&lt;BR /&gt;*calculate measurement time from variable name, generally bad practice to store data in column headers;
dm_time=input(compress(_name_, , 'kd'), 8.);

run;

proc means data=long nway mean n std;
class id disease ;
var col1;
where dm_time &amp;lt; time or time=.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 19 Oct 2016 20:30:23 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2016-10-19T20:30:23Z</dc:date>
    <item>
      <title>Obtaining mean for variable number of variables per subject</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Obtaining-mean-for-variable-number-of-variables-per-subject/m-p/305753#M65253</link>
      <description>&lt;P&gt;Dear All,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am working on a project for which I have to get mean levels&amp;nbsp;of a disease marker (let's call it DM). The issue is that the mean must only include measures before the individual developed disease, and the time each person may have developed disease differs. So, for example, if ID=1 has 7 time points where DM was assessed and this person developed disease at time 5, I would need the mean of DM from time 1 to time 4. If ID=2 has DM measured from time 1 to 6 and the person developed disease at time 3, I'd&amp;nbsp;need the mean DM from time 1 to time 2. If another person did not develop disease, I'd get the mean of all the available DM measures. I am really stuck on how to do this. Any help would be greatly appreciated!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Many thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Oct 2016 19:49:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Obtaining-mean-for-variable-number-of-variables-per-subject/m-p/305753#M65253</guid>
      <dc:creator>TDC</dc:creator>
      <dc:date>2016-10-19T19:49:27Z</dc:date>
    </item>
    <item>
      <title>Re: Obtaining mean for variable number of variables per subject</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Obtaining-mean-for-variable-number-of-variables-per-subject/m-p/305755#M65255</link>
      <description>&lt;P&gt;It all depends on your data structure. Please post an example.&lt;/P&gt;</description>
      <pubDate>Wed, 19 Oct 2016 19:57:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Obtaining-mean-for-variable-number-of-variables-per-subject/m-p/305755#M65255</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-10-19T19:57:39Z</dc:date>
    </item>
    <item>
      <title>Re: Obtaining mean for variable number of variables per subject</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Obtaining-mean-for-variable-number-of-variables-per-subject/m-p/305762#M65259</link>
      <description>&lt;P&gt;Dear PGStats,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a wide data set (an&amp;nbsp;example is given below).&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; test;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;input&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;&lt;FONT face="Courier New" size="3"&gt; id dm1-dm7 disease time;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;cards&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;1 35 38 40 43 53 55 55 1 7&lt;/P&gt;&lt;P&gt;2 25 29 20&amp;nbsp;. 25 30 28 0 .&lt;/P&gt;&lt;P&gt;3 38 40 40 50 48 30 35 1 5&lt;/P&gt;&lt;P&gt;4 44 43 41 39 38&amp;nbsp;. 37 1 3&lt;/P&gt;&lt;P&gt;5 28 25 30 28 30 27 31 0 .&lt;/P&gt;&lt;P&gt;6 38 40 45&amp;nbsp;. 50 29 28 1 5&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Above, ID 1 developed disease at the 7th time point, so I'd need to get the means of DM1-DM6.&lt;/P&gt;&lt;P&gt;ID 2 did not develop disease, so I'd need the means of all available DM values.&lt;/P&gt;&lt;P&gt;ID 3 developed disease at time 5 so I'd get the mean of DM from times 1 to 4, etc.&lt;/P&gt;&lt;P&gt;Thank you for any help!&lt;/P&gt;&lt;P&gt;TDC.&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Oct 2016 20:07:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Obtaining-mean-for-variable-number-of-variables-per-subject/m-p/305762#M65259</guid>
      <dc:creator>TDC</dc:creator>
      <dc:date>2016-10-19T20:07:48Z</dc:date>
    </item>
    <item>
      <title>Re: Obtaining mean for variable number of variables per subject</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Obtaining-mean-for-variable-number-of-variables-per-subject/m-p/305771#M65264</link>
      <description>&lt;P&gt;In case there is only one record per ID (and MEAN means AVERAGE)&lt;/P&gt;
&lt;P&gt;than you can do it by:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#000080"&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; test;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;input&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;&lt;FONT face="Courier New" size="3"&gt; id dm1-dm7 disease time;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;&lt;FONT face="Courier New" size="3"&gt;&amp;nbsp; array dm {*} dm1-dm7;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;&lt;FONT face="Courier New" size="3"&gt;&amp;nbsp; sum_dm = 0;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;&lt;FONT face="Courier New" size="3"&gt;&amp;nbsp; if time &amp;gt; 1 then do;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;&lt;FONT face="Courier New" size="3"&gt;&amp;nbsp; &amp;nbsp; do i=1 to (time -1);&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;&lt;FONT face="Courier New" size="3"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;sum_dm = sum(of sum_dm, dm(i));&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;&lt;FONT face="Courier New" size="3"&gt;&amp;nbsp; &amp;nbsp; end;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;&lt;FONT face="Courier New" size="3"&gt;&amp;nbsp; &amp;nbsp; MEAN = sum_dm / (time -1);&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;&lt;FONT face="Courier New" size="3"&gt;&amp;nbsp; end; &lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;&lt;FONT face="Courier New" size="3"&gt;&amp;nbsp; else MEAN = .;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;cards&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;1 35 38 40 43 53 55 55 1 7&lt;/P&gt;
&lt;P&gt;2 25 29 20&amp;nbsp;. 25 30 28 0 .&lt;/P&gt;
&lt;P&gt;3 38 40 40 50 48 30 35 1 5&lt;/P&gt;
&lt;P&gt;4 44 43 41 39 38&amp;nbsp;. 37 1 3&lt;/P&gt;
&lt;P&gt;5 28 25 30 28 30 27 31 0 .&lt;/P&gt;
&lt;P&gt;6 38 40 45&amp;nbsp;. 50 29 28 1 5&lt;/P&gt;
&lt;P&gt;;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Oct 2016 20:29:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Obtaining-mean-for-variable-number-of-variables-per-subject/m-p/305771#M65264</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2016-10-19T20:29:09Z</dc:date>
    </item>
    <item>
      <title>Re: Obtaining mean for variable number of variables per subject</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Obtaining-mean-for-variable-number-of-variables-per-subject/m-p/305772#M65265</link>
      <description>&lt;P&gt;Something like this perhaps:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data want;
   set test;
   array dm dm1-dm7;
   if disease and not missing(time) then do;
      do i=1 to time;
         dmsum=sum(dmsum,dm[i]);
      end;
      dmmean= dmsum/time;
   end;
   drop i;
run;   
      
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 19 Oct 2016 20:29:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Obtaining-mean-for-variable-number-of-variables-per-subject/m-p/305772#M65265</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-10-19T20:29:11Z</dc:date>
    </item>
    <item>
      <title>Re: Obtaining mean for variable number of variables per subject</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Obtaining-mean-for-variable-number-of-variables-per-subject/m-p/305773#M65266</link>
      <description>&lt;P&gt;Transpose and use proc means.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=test;
by id disease time;
run;

proc transpose data=test out=long;
by id disease time;
var dm1-dm7;
run;

data long;
set long;&lt;BR /&gt;*calculate measurement time from variable name, generally bad practice to store data in column headers;
dm_time=input(compress(_name_, , 'kd'), 8.);

run;

proc means data=long nway mean n std;
class id disease ;
var col1;
where dm_time &amp;lt; time or time=.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 19 Oct 2016 20:30:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Obtaining-mean-for-variable-number-of-variables-per-subject/m-p/305773#M65266</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-10-19T20:30:23Z</dc:date>
    </item>
    <item>
      <title>Re: Obtaining mean for variable number of variables per subject</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Obtaining-mean-for-variable-number-of-variables-per-subject/m-p/305806#M65284</link>
      <description>&lt;P&gt;If you want to keep it wide:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set test;
array dm dm1-dm7;
array x{7};
do i = 1 to coalesce(time, dim(dm)+1) - 1;
    x{i} = dm{i};
    end;
meanDm = mean(of x{*});
drop i x:;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 19 Oct 2016 21:43:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Obtaining-mean-for-variable-number-of-variables-per-subject/m-p/305806#M65284</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-10-19T21:43:02Z</dc:date>
    </item>
    <item>
      <title>Re: Obtaining mean for variable number of variables per subject</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Obtaining-mean-for-variable-number-of-variables-per-subject/m-p/305807#M65285</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/88384"&gt;@Shmuel﻿&lt;/a&gt;, your denominator will be wrong if missing dm values occur before time. If you want to calculate your own average, you must keep track of the count of non-missing dm values as well as the sum of dm values.&lt;/P&gt;</description>
      <pubDate>Wed, 19 Oct 2016 21:48:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Obtaining-mean-for-variable-number-of-variables-per-subject/m-p/305807#M65285</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-10-19T21:48:31Z</dc:date>
    </item>
    <item>
      <title>Re: Obtaining mean for variable number of variables per subject</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Obtaining-mean-for-variable-number-of-variables-per-subject/m-p/305924#M65334</link>
      <description>&lt;P&gt;Thank you All so much!! I really appreciate your help!!&lt;/P&gt;&lt;P&gt;TDC.&lt;/P&gt;</description>
      <pubDate>Thu, 20 Oct 2016 12:10:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Obtaining-mean-for-variable-number-of-variables-per-subject/m-p/305924#M65334</guid>
      <dc:creator>TDC</dc:creator>
      <dc:date>2016-10-20T12:10:20Z</dc:date>
    </item>
  </channel>
</rss>

