<?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 Array function for Area under the Curve with multiple variables? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Array-function-for-Area-under-the-Curve-with-multiple-variables/m-p/893833#M353096</link>
    <description>&lt;P&gt;I'm calculating the area under the curve for time series measurements for n individuals and I want to do this for more than ten different variables:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data want; set have; by id;&lt;/P&gt;&lt;P&gt;var1area+dif(date)*mean(var1,lag(var1));&lt;SPAN&gt;if&lt;/SPAN&gt; first.id &lt;SPAN&gt;then&lt;/SPAN&gt; var1area=&lt;SPAN&gt;&lt;STRONG&gt;0&lt;/STRONG&gt;&lt;/SPAN&gt;;&lt;/P&gt;&lt;P&gt;var2area+dif(date)*mean(var2,lag(var1));&lt;SPAN&gt;if&lt;/SPAN&gt; first.id &lt;SPAN&gt;then&lt;/SPAN&gt; var2area=&lt;SPAN&gt;&lt;STRONG&gt;0&lt;/STRONG&gt;&lt;/SPAN&gt;;&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;It runs well, however, when I tried to put this into an array function it didn't work most likely because of the "if first.id then _var(i)area =0. All columns except the first one will give me 0 in every observation.&lt;/P&gt;&lt;P&gt;Does anyone have a solution?&lt;/P&gt;</description>
    <pubDate>Tue, 12 Sep 2023 15:55:40 GMT</pubDate>
    <dc:creator>donanlb</dc:creator>
    <dc:date>2023-09-12T15:55:40Z</dc:date>
    <item>
      <title>Array function for Area under the Curve with multiple variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-function-for-Area-under-the-Curve-with-multiple-variables/m-p/893833#M353096</link>
      <description>&lt;P&gt;I'm calculating the area under the curve for time series measurements for n individuals and I want to do this for more than ten different variables:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data want; set have; by id;&lt;/P&gt;&lt;P&gt;var1area+dif(date)*mean(var1,lag(var1));&lt;SPAN&gt;if&lt;/SPAN&gt; first.id &lt;SPAN&gt;then&lt;/SPAN&gt; var1area=&lt;SPAN&gt;&lt;STRONG&gt;0&lt;/STRONG&gt;&lt;/SPAN&gt;;&lt;/P&gt;&lt;P&gt;var2area+dif(date)*mean(var2,lag(var1));&lt;SPAN&gt;if&lt;/SPAN&gt; first.id &lt;SPAN&gt;then&lt;/SPAN&gt; var2area=&lt;SPAN&gt;&lt;STRONG&gt;0&lt;/STRONG&gt;&lt;/SPAN&gt;;&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;It runs well, however, when I tried to put this into an array function it didn't work most likely because of the "if first.id then _var(i)area =0. All columns except the first one will give me 0 in every observation.&lt;/P&gt;&lt;P&gt;Does anyone have a solution?&lt;/P&gt;</description>
      <pubDate>Tue, 12 Sep 2023 15:55:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-function-for-Area-under-the-Curve-with-multiple-variables/m-p/893833#M353096</guid>
      <dc:creator>donanlb</dc:creator>
      <dc:date>2023-09-12T15:55:40Z</dc:date>
    </item>
    <item>
      <title>Re: Array function for Area under the Curve with multiple variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-function-for-Area-under-the-Curve-with-multiple-variables/m-p/893839#M353099</link>
      <description>&lt;P&gt;If I am understanding you properly (and I may not be)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You want an ARRAY statement, such as&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;array vararea var1area var2area ... ; /* You type the rest */
array var var1 var2 ... ; /* You type the rest */&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;and then call the arrays via&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;vararea(i)+dif(date)*mean(var(i),lag(var(i)));if first.id then vararea(i)=0;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;BUT — IMPORTANT — make your life simpler, don't name variables var1area var2area ... name them vararea1, vararea2, ... or even better yet area1, area2, ... with numeric suffixes (not the number inside the variable name like var1area)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In which case everything is simpler!! Then you can use SAS variable lists, such as below:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;array area area1-area10; 
array var var1-var10;

/* Call the array */

area(i)+dif(date)*mean(var(i),lag(var(i)));if first.id then area(i)=0;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Sep 2023 16:34:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-function-for-Area-under-the-Curve-with-multiple-variables/m-p/893839#M353099</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-09-12T16:34:46Z</dc:date>
    </item>
    <item>
      <title>Re: Array function for Area under the Curve with multiple variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-function-for-Area-under-the-Curve-with-multiple-variables/m-p/893852#M353105</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/452451"&gt;@donanlb&lt;/a&gt;&amp;nbsp;and welcome to the SAS Support Communities!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The problem is that &lt;FONT face="courier new,courier"&gt;dif(date)=0&amp;nbsp;&lt;/FONT&gt;starting from the second iteration of the loop because the date doesn't change. Assign &lt;FONT face="courier new,courier"&gt;dif(date)&lt;/FONT&gt; to a variable before the loop and then use that variable in the loop.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;data want(drop=i d);
array var[15];
array AUC[15];
set have;
by id;
&lt;FONT color="#3366FF"&gt;&lt;STRONG&gt;d=ifn(_n_&amp;gt;1,dif(date),0);&lt;/STRONG&gt;&lt;/FONT&gt;
do i=1 to dim(var);
  AUC[i]+&lt;FONT color="#3366FF"&gt;&lt;STRONG&gt;d&lt;/STRONG&gt;&lt;/FONT&gt;*mean(var[i],lag(var[i]));
  if first.id then AUC[i]=0;
end;
*if last.id;
*drop var: date;
run;&lt;/PRE&gt;</description>
      <pubDate>Tue, 12 Sep 2023 17:02:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-function-for-Area-under-the-Curve-with-multiple-variables/m-p/893852#M353105</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2023-09-12T17:02:42Z</dc:date>
    </item>
    <item>
      <title>Re: Array function for Area under the Curve with multiple variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-function-for-Area-under-the-Curve-with-multiple-variables/m-p/894017#M353184</link>
      <description>&lt;P&gt;Thanks a lot - that worked!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I additionally inserted an id count before the suggested variable - otherwise it would always have calculated the difference from the start of the sheet instead of from the start of each id. In the end I inserted another array to calculate the time-averaged mean by using the timelag variable that I had already calculated before for each id from the first to the individual observation.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This will save me a lot of time during the next days &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Here's the code:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data want ;set have;count+1;by id;
if first.id then count=1;
d=ifn(count&amp;gt;1,dif(date),0);
array _var(*) var1-var17;
array _area(*) area1-area17;
array _avevar(*) avevar1-avevar17;
do i=1 to dim(_var);
_area(i)+d*mean(_var(i),lag(_var(i)));if first.id then _area(i)=0;
_avevar(i)=_area(i)/timelag;
if _avevar(i)=. then _avevar(i)=_var(i);
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 13 Sep 2023 09:56:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-function-for-Area-under-the-Curve-with-multiple-variables/m-p/894017#M353184</guid>
      <dc:creator>donanlb</dc:creator>
      <dc:date>2023-09-13T09:56:43Z</dc:date>
    </item>
    <item>
      <title>Re: Array function for Area under the Curve with multiple variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-function-for-Area-under-the-Curve-with-multiple-variables/m-p/894039#M353194</link>
      <description>&lt;P&gt;You're welcome. Glad to see that my code worked for your data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that the new variable COUNT is not needed. The only reason for my &lt;FONT face="courier new,courier"&gt;ifn(_n_&amp;gt;1, ...)&lt;/FONT&gt; construct was to avoid the unnecessary note "Missing values were generated ..." in the log. This note would occur during the AUC calculation due to the missing value returned by the DIF function when it is called for the first time, i.e., when &lt;FONT face="courier new,courier"&gt;_n_=1&lt;/FONT&gt;. (I should have explained this.) The results in dataset WANT would have been the same, had I just written&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;d=dif(date);&lt;/PRE&gt;
&lt;P&gt;The value of variable &lt;FONT face="courier new,courier"&gt;d&lt;/FONT&gt; in the first observation of an ID, be it missing or incorrect, does not affect the results anyway because&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;if first.id then AUC[i]=0;&lt;/PRE&gt;
&lt;P&gt;and the variable is dropped.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your calculation of &lt;FONT face="courier new,courier"&gt;_avevar(i)&lt;/FONT&gt; may cause unnecessary notes in the log (such as "Division by zero detected ..." or "Mathematical operations could not be performed ..."). I think you could avoid them (without changing the results) by using conditional assignment statements:&lt;/P&gt;
&lt;PRE&gt;if first.id then _avevar(i)=_var(i);
else _avevar(i)=_area(i)/timelag;&lt;/PRE&gt;</description>
      <pubDate>Wed, 13 Sep 2023 12:40:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-function-for-Area-under-the-Curve-with-multiple-variables/m-p/894039#M353194</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2023-09-13T12:40:38Z</dc:date>
    </item>
    <item>
      <title>Re: Array function for Area under the Curve with multiple variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-function-for-Area-under-the-Curve-with-multiple-variables/m-p/894224#M353239</link>
      <description>Thank you for clarifying!</description>
      <pubDate>Thu, 14 Sep 2023 07:22:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-function-for-Area-under-the-Curve-with-multiple-variables/m-p/894224#M353239</guid>
      <dc:creator>donanlb</dc:creator>
      <dc:date>2023-09-14T07:22:17Z</dc:date>
    </item>
  </channel>
</rss>

