<?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: Use SAS number range list in an expression in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Use-SAS-number-range-list-in-an-expression/m-p/399285#M96696</link>
    <description>&lt;P&gt;Can you post (an extract) of dataset HAVE as data step so simplify creating a solution?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You need %eval-macro-function in your array definition:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;array combined {%eval(2* &amp;amp;num_leases.)} ...&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 27 Sep 2017 18:05:25 GMT</pubDate>
    <dc:creator>error_prone</dc:creator>
    <dc:date>2017-09-27T18:05:25Z</dc:date>
    <item>
      <title>Use SAS number range list in an expression</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-SAS-number-range-list-in-an-expression/m-p/399279#M96695</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am looking to dynamically reference a variable name within an expression.&lt;/P&gt;&lt;P&gt;Examples below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;DATA out;
	set have;

	array values {&amp;amp;num_leases.};
	array dates {&amp;amp;num_leases.};
	array combined {2* &amp;amp;num_leases.} Var1-Var&amp;amp;num_leases.;
RUN&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In the above, I would like to create&amp;nbsp;arrays with a length stored in the variable num_leases. For the "combined" array, I would like to then create variables names Var1 Var2... VarN, where N is the number of leases stored in num_leases.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I then go on to store values in these arrays, and then I would like to find the last non-missing (i.e. ne .) entry in the "combined" array and use all non-missing elements in an expression, as below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;do i = 1 to &amp;amp;num_leases.;
	if combined{i} = . then leave;
end;
IRR = finance('xirr',combined{1}:combined{i});
output;&lt;/PRE&gt;&lt;P&gt;So, in the above, I am looping through all elements in&amp;nbsp;the "combined" array, and, when I find the first missing element, I am storing where in the array is it (i.e. element i). I then want to pass the non-missing elements in this array to the&amp;nbsp;finance XIRR function to calculate an IRR.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Would anyone be able to help me understand how to dynamically reference variable names, and then pass them to an expression, as per the above?&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;&lt;P&gt;&amp;nbsp;&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;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Sep 2017 17:42:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-SAS-number-range-list-in-an-expression/m-p/399279#M96695</guid>
      <dc:creator>MikeFranz</dc:creator>
      <dc:date>2017-09-27T17:42:32Z</dc:date>
    </item>
    <item>
      <title>Re: Use SAS number range list in an expression</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-SAS-number-range-list-in-an-expression/m-p/399285#M96696</link>
      <description>&lt;P&gt;Can you post (an extract) of dataset HAVE as data step so simplify creating a solution?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You need %eval-macro-function in your array definition:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;array combined {%eval(2* &amp;amp;num_leases.)} ...&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Sep 2017 18:05:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-SAS-number-range-list-in-an-expression/m-p/399285#M96696</guid>
      <dc:creator>error_prone</dc:creator>
      <dc:date>2017-09-27T18:05:25Z</dc:date>
    </item>
    <item>
      <title>Re: Use SAS number range list in an expression</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-SAS-number-range-list-in-an-expression/m-p/399286#M96697</link>
      <description>&lt;P&gt;Just to get the ball rolling ...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So far you have a lot of the syntax correct.&amp;nbsp; But I'm not sure about the SAS code.&amp;nbsp; Here are some questions.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do the arrays you mention represent variables that already exist, or variables that&amp;nbsp; you want to create?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why 2 * &amp;amp;n_leases for one of the arrays?&amp;nbsp; Actually, the correct syntax for that would be to have macro language perform the math and insert the result as the number of array elements:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;{ %eval(2 * &amp;amp;n_leases.) }&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to know the number of nonmissing elements, you don't need arrays.&amp;nbsp; You can code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;i = n(of var1 - var&amp;amp;n_leases.) ;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you name an array and provide the number of elements, those elements can be named automatically.&amp;nbsp; For example, these statements would provide identical results:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;array var {&amp;amp;n_leases} var1 - var&amp;amp;n_leases.;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;array var {&amp;amp;n_leases} ;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When the array elements are not named, they will automatically be the name of the array with a numeric suffix.&lt;/P&gt;</description>
      <pubDate>Wed, 27 Sep 2017 18:09:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-SAS-number-range-list-in-an-expression/m-p/399286#M96697</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-09-27T18:09:16Z</dc:date>
    </item>
    <item>
      <title>Re: Use SAS number range list in an expression</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-SAS-number-range-list-in-an-expression/m-p/399291#M96698</link>
      <description>&lt;P&gt;You have a fundamental logic&amp;nbsp;problem with this line:&lt;/P&gt;
&lt;P&gt;array combined {2* &amp;amp;num_leases.} Var1-Var&amp;amp;num_leases.;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You apparently want to create 2 time &amp;amp;num_leases variable but only provide &amp;amp;num_leases&amp;nbsp; variables. If you provide a number of elements in {} then the number of names in a provided list of variables has to match. &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;if you use&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;array var &lt;FONT face="SAS Monospace" size="2"&gt;{&lt;/FONT&gt;&lt;FONT color="#ff0080" face="SAS Monospace" size="2"&gt;%eval&lt;/FONT&gt;&lt;FONT face="SAS Monospace" size="2"&gt;(&lt;/FONT&gt;&lt;FONT color="#008080" face="SAS Monospace" size="2"&gt;2&lt;/FONT&gt;&lt;FONT face="SAS Monospace" size="2"&gt;*&amp;amp;num_leases)}; &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="SAS Monospace" size="2"&gt;That will create variables named&amp;nbsp;Var1-Var2*num_leases &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="SAS Monospace" size="2"&gt;So either provide enough variable names or use a different name for the array to create the matching number of variables. &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="SAS Monospace" size="2"&gt;But I don't think you need that combined array and the way you were attempting to use it was incorrect.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="SAS Monospace" size="2"&gt;See this for an example of useing the SIRR with arrays.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;FONT face="SAS Monospace" size="2"&gt;data _null_;
   v1=−10000; d1=mdy(1, 1, 2008);
   v2=2750; d2=mdy(3, 1, 2008);
   v3=4250; d3=mdy(10, 30, 2008);
   v4=3250; d4=mdy(2, 15, 2009);
   v5=2750; d5=mdy(4, 1, 2009);
   array values v1-v5;
   array dates d1-d5;
   r=finance('xirr', of values(*), of dates(*), 0.1);
   put r=;
run;&lt;/FONT&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;FONT face="SAS Monospace" size="2"&gt;So if you can successfully create and populate your values and dates arrays that all you need.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="SAS Monospace" size="2"&gt;If you only want to use a subset of the elements in the values and dates arrays then you may wish to copy them into other arrays with the unwanted elements set to missing.&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Sep 2017 18:15:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-SAS-number-range-list-in-an-expression/m-p/399291#M96698</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-09-27T18:15:37Z</dc:date>
    </item>
    <item>
      <title>Re: Use SAS number range list in an expression</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-SAS-number-range-list-in-an-expression/m-p/399294#M96699</link>
      <description>Ah ok, that makes sense, I'll get rid of the superfluous code. Thanks!&lt;BR /&gt;</description>
      <pubDate>Wed, 27 Sep 2017 18:24:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-SAS-number-range-list-in-an-expression/m-p/399294#M96699</guid>
      <dc:creator>MikeFranz</dc:creator>
      <dc:date>2017-09-27T18:24:44Z</dc:date>
    </item>
    <item>
      <title>Re: Use SAS number range list in an expression</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-SAS-number-range-list-in-an-expression/m-p/399295#M96700</link>
      <description>Ok, I haven't done macro coding, but I'll update the line to look like this. Thanks &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;</description>
      <pubDate>Wed, 27 Sep 2017 18:25:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-SAS-number-range-list-in-an-expression/m-p/399295#M96700</guid>
      <dc:creator>MikeFranz</dc:creator>
      <dc:date>2017-09-27T18:25:05Z</dc:date>
    </item>
    <item>
      <title>Re: Use SAS number range list in an expression</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-SAS-number-range-list-in-an-expression/m-p/399296#M96701</link>
      <description>Thanks for the response. Understand everything until the final lines. How would I go about creating a new array from the existing ones, and setting the unwanted values to missing? And would the IRR function work with missing values? Thanks!</description>
      <pubDate>Wed, 27 Sep 2017 18:26:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-SAS-number-range-list-in-an-expression/m-p/399296#M96701</guid>
      <dc:creator>MikeFranz</dc:creator>
      <dc:date>2017-09-27T18:26:28Z</dc:date>
    </item>
    <item>
      <title>Re: Use SAS number range list in an expression</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-SAS-number-range-list-in-an-expression/m-p/399316#M96709</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/109914"&gt;@MikeFranz&lt;/a&gt; wrote:&lt;BR /&gt;Thanks for the response. Understand everything until the final lines. How would I go about creating a new array from the existing ones, and setting the unwanted values to missing? And would the IRR function work with missing values? Thanks!&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Actually it looks like we need to set the unwanted values to 0.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data _null_;
   v1=−10000; d1=mdy(1, 1, 2008);
   v2=2750; d2=mdy(3, 1, 2008);
   v3=4250; d3=mdy(10, 30, 2008);
   v4=3250; d4=mdy(2, 15, 2009);
   v5=2750; d5=mdy(4, 1, 2009);
   array values v1-v5;
   array dates d1-d5;
   array vs {5};
   array ds {5};
   /* to copy the first 3 of each array into a subset*/
   do i=1 to 3;
      vs[i] = values[i];
      ds[i] = dates[i];
   end;
   do i=4 to dim(values);
      vs[i] = 0;
      ds[i] = 0;
   end;
   r=finance('xirr', of values(*), of dates(*), 0.1);
   r2=finance('xirr',of vs(*),of ds(*), 0.1);
   /* the next shows that the zeroes in the vs,ds array
      yield same as the specific variables
   */
   r3=finance('xirr',vs1,vs2,vs3,ds1,ds2,ds3, 0.1);
   put r= r2= r3=;
run;&lt;/PRE&gt;</description>
      <pubDate>Wed, 27 Sep 2017 19:28:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-SAS-number-range-list-in-an-expression/m-p/399316#M96709</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-09-27T19:28:14Z</dc:date>
    </item>
    <item>
      <title>Re: Use SAS number range list in an expression</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-SAS-number-range-list-in-an-expression/m-p/399443#M96749</link>
      <description>&lt;P&gt;Mike,&lt;/P&gt;&lt;P&gt;Looks like what you want to do is something like this:&lt;/P&gt;&lt;PRE&gt;Data out;
  set have;
  array dates (*) date1-date&amp;amp;Num_leases;
  array values(*) value1-value&amp;amp;Num_leases;
/* code to populate arrays here */
  IRR=finance('XIRR',&amp;lt;dynamic portion of values&amp;gt;,&amp;lt;dynamic portion of dates&amp;gt;);
run;

&lt;/PRE&gt;&lt;P&gt;Unfortunately, there is no way you can refer to a dynamic number of values in a function call like that. One way out of that could be to code a select statement:&lt;/P&gt;&lt;PRE&gt;select (n(of values(*)); /* select on non-missing values */
  when(0); /* not sure what to do here... */
  when(1) IRR=finance('XIRR',value1,date1);
  when(2) IRR=finance('XIRR',of value1-value2,of date1-date2);
  when(3) IRR=finance('XIRR',of value1-value3,of date1-date3);&lt;/PRE&gt;&lt;P&gt;Which can, of course, be coded as a macro. But I think the easy way out is to replace the missing values with zeros:&lt;/P&gt;&lt;PRE&gt;do i=1 to dim(dates);
&amp;nbsp; if missing(values(i)) then do;&lt;BR /&gt;    values(i)=0;&lt;BR /&gt;    dates(i)=0; /* does not matter what the date is, but it cannot be missing */&lt;BR /&gt;    end; &lt;BR /&gt;  end;&lt;/PRE&gt;&lt;P&gt;The&amp;nbsp;FINANCE function does not accept missing values, but any number of zero values&amp;nbsp;does not change the result, so you can then just do&lt;/P&gt;&lt;PRE&gt;IRR=finance('XIRR',of values(*),of dates(*));&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Sep 2017 09:49:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-SAS-number-range-list-in-an-expression/m-p/399443#M96749</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2017-09-28T09:49:37Z</dc:date>
    </item>
  </channel>
</rss>

