<?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: Calculations in array with macro variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Calculations-in-array-with-macro-variables/m-p/641408#M191156</link>
    <description>&lt;P&gt;&lt;STRONG&gt;%let v1 = 10;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;%let v2 = 15;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;%let v1 = 13; &amp;lt;- should be v3?&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What does not working mean? Did you get any errors or warnings in the log?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Set the debugging options, run your code and post the log if you have issues after you fix the issue noted above.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options mprint symbolgen;

*your code;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/152501"&gt;@alexgonzalez&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi there,&lt;/P&gt;
&lt;P&gt;I’m trying to do some calculation using previously specified macro variables (i.e. v1, v2, and v3) within a macro. I’ve the following&amp;nbsp; code, but it’s not working.&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;%let v1 = 10;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;%let v2 = 15;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;%let v1 = 13;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&amp;nbsp;&lt;/STRONG&gt;&lt;STRONG&gt;data&lt;/STRONG&gt; z2;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; set z;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; array diff {&lt;STRONG&gt;3&lt;/STRONG&gt;} diff1-diff3;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; array vars {&lt;STRONG&gt;3&lt;/STRONG&gt;} a b c;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %do i = &lt;STRONG&gt;1&lt;/STRONG&gt; %to &lt;STRONG&gt;3&lt;/STRONG&gt;;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;diff{i} = nt{i}-&amp;amp;&amp;amp;v&amp;amp;i&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;%end;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;STRONG&gt;run&lt;/STRONG&gt;;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have tried many different options but nothing is working. Any recommendation please?&lt;/P&gt;
&lt;P&gt;Thank you very much.&lt;/P&gt;
&lt;P&gt;A.G.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;STRONG&gt;&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;</description>
    <pubDate>Mon, 20 Apr 2020 16:38:54 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2020-04-20T16:38:54Z</dc:date>
    <item>
      <title>Calculations in array with macro variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculations-in-array-with-macro-variables/m-p/641405#M191153</link>
      <description>&lt;P&gt;Hi there,&lt;/P&gt;
&lt;P&gt;I’m trying to do some calculation using previously specified macro variables (i.e. v1, v2, and v3) within a macro. I’ve the following&amp;nbsp; code, but it’s not working.&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;%let v1 = 10;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;%let v2 = 15;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;%let v1 = 13;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&amp;nbsp;&lt;/STRONG&gt;&lt;STRONG&gt;data&lt;/STRONG&gt; z2;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; set z;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; array diff {&lt;STRONG&gt;3&lt;/STRONG&gt;} diff1-diff3;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; array vars {&lt;STRONG&gt;3&lt;/STRONG&gt;} a b c;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %do i = &lt;STRONG&gt;1&lt;/STRONG&gt; %to &lt;STRONG&gt;3&lt;/STRONG&gt;;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;diff{i} = nt{i}-&amp;amp;&amp;amp;v&amp;amp;i&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;%end;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;STRONG&gt;run&lt;/STRONG&gt;;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have tried many different options but nothing is working. Any recommendation please?&lt;/P&gt;
&lt;P&gt;Thank you very much.&lt;/P&gt;
&lt;P&gt;A.G.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 20 Apr 2020 16:31:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculations-in-array-with-macro-variables/m-p/641405#M191153</guid>
      <dc:creator>alexgonzalez</dc:creator>
      <dc:date>2020-04-20T16:31:13Z</dc:date>
    </item>
    <item>
      <title>Re: Calculations in array with macro variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculations-in-array-with-macro-variables/m-p/641408#M191156</link>
      <description>&lt;P&gt;&lt;STRONG&gt;%let v1 = 10;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;%let v2 = 15;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;%let v1 = 13; &amp;lt;- should be v3?&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What does not working mean? Did you get any errors or warnings in the log?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Set the debugging options, run your code and post the log if you have issues after you fix the issue noted above.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options mprint symbolgen;

*your code;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/152501"&gt;@alexgonzalez&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi there,&lt;/P&gt;
&lt;P&gt;I’m trying to do some calculation using previously specified macro variables (i.e. v1, v2, and v3) within a macro. I’ve the following&amp;nbsp; code, but it’s not working.&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;%let v1 = 10;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;%let v2 = 15;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;%let v1 = 13;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&amp;nbsp;&lt;/STRONG&gt;&lt;STRONG&gt;data&lt;/STRONG&gt; z2;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; set z;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; array diff {&lt;STRONG&gt;3&lt;/STRONG&gt;} diff1-diff3;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; array vars {&lt;STRONG&gt;3&lt;/STRONG&gt;} a b c;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %do i = &lt;STRONG&gt;1&lt;/STRONG&gt; %to &lt;STRONG&gt;3&lt;/STRONG&gt;;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;diff{i} = nt{i}-&amp;amp;&amp;amp;v&amp;amp;i&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;%end;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;STRONG&gt;run&lt;/STRONG&gt;;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have tried many different options but nothing is working. Any recommendation please?&lt;/P&gt;
&lt;P&gt;Thank you very much.&lt;/P&gt;
&lt;P&gt;A.G.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;STRONG&gt;&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 20 Apr 2020 16:38:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculations-in-array-with-macro-variables/m-p/641408#M191156</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-04-20T16:38:54Z</dc:date>
    </item>
    <item>
      <title>Re: Calculations in array with macro variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculations-in-array-with-macro-variables/m-p/641409#M191157</link>
      <description>&lt;P&gt;And I would recommend simplifying your code to remove most of the macros and use a temporary array.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let v1 = 10;
%let v2 = 15;
%let v3 = 13;

data z2;
set z;           
array diff {3} diff1-diff3;
array vars {3} a b c;
array _v(3) _temporary_ (&amp;amp;v1 &amp;amp;v2 &amp;amp;v3);

       do i = 1 to 3;
            diff{i} = nt{i} -  _v(i)
       end;     
 run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 20 Apr 2020 16:42:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculations-in-array-with-macro-variables/m-p/641409#M191157</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-04-20T16:42:34Z</dc:date>
    </item>
    <item>
      <title>Re: Calculations in array with macro variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculations-in-array-with-macro-variables/m-p/641411#M191158</link>
      <description>&lt;P&gt;please try&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let v1 = 10;
%let v2 = 15;
%let v3 = 13;

data z;
input Var$ value;
cards;
a 20
b 17
c 30
;

proc transpose data=z out=z1;
id var;
var value;
run;


%macro test;
data z2;
set z1;
  array diff{3} diff1-diff3;
  array vars{3} a b c;
 %do i = 1 %to 3;
            diff(&amp;amp;i) = vars(&amp;amp;i)-&amp;amp;&amp;amp;v&amp;amp;i;
       %end;     
run;
%mend;

%test;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 20 Apr 2020 16:43:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculations-in-array-with-macro-variables/m-p/641411#M191158</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2020-04-20T16:43:20Z</dc:date>
    </item>
    <item>
      <title>Re: Calculations in array with macro variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculations-in-array-with-macro-variables/m-p/641424#M191166</link>
      <description>&lt;P&gt;macro instructions are applied at compilation of the data step, so if you expect a loop such as DO to reference VARIABLES in the data set you do no want want %do, %end.&lt;/P&gt;</description>
      <pubDate>Mon, 20 Apr 2020 17:12:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculations-in-array-with-macro-variables/m-p/641424#M191166</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-04-20T17:12:11Z</dc:date>
    </item>
    <item>
      <title>Re: Calculations in array with macro variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculations-in-array-with-macro-variables/m-p/641426#M191168</link>
      <description>&lt;P&gt;Hi Reeza,&lt;/P&gt;
&lt;P&gt;This works perfectly, but since I do not know for sure the number of variables that will be in the temporary array you suggest, I'm not sure then how to make it work. See below:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;array _v {&amp;amp;number_nutr.} _temporary_ (&amp;amp;v1 - &amp;amp;&amp;amp;v&amp;amp;number_nutr);&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; %do i = 1 %to &amp;amp;number_nutr.; &lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;diff{i} = nt{i}-_v(i);&lt;BR /&gt;%end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As you can see in the previous code, I'm passing in the array&amp;nbsp;&amp;amp;number_nutr variables. When using the - sign to specify all the variables, then it gives an error (WARNING: Partial value initialization of the array _v.). Any further suggestion would be very much appreciated.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 20 Apr 2020 17:28:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculations-in-array-with-macro-variables/m-p/641426#M191168</guid>
      <dc:creator>alexgonzalez</dc:creator>
      <dc:date>2020-04-20T17:28:13Z</dc:date>
    </item>
    <item>
      <title>Re: Calculations in array with macro variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculations-in-array-with-macro-variables/m-p/641429#M191169</link>
      <description>Why do you have so many macro variables?  If you want to use those values together put them into ONE macro varaible.</description>
      <pubDate>Mon, 20 Apr 2020 17:33:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculations-in-array-with-macro-variables/m-p/641429#M191169</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-04-20T17:33:16Z</dc:date>
    </item>
    <item>
      <title>Re: Calculations in array with macro variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculations-in-array-with-macro-variables/m-p/641430#M191170</link>
      <description>&lt;P&gt;Aren't you the same user that asked how to get the values out of data and into macro variables?&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Programming/Assigning-macro-variables-to-input-parameters/m-p/641333" target="_blank"&gt;https://communities.sas.com/t5/SAS-Programming/Assigning-macro-variables-to-input-parameters/m-p/641333&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why not just leave the values in the dataset to begin with.&lt;/P&gt;
&lt;P&gt;Looks like you haven't explained your real problem, just the problems you are having with the design you have chosen.&amp;nbsp;&lt;A href="http://xyproblem.info/" target="_blank"&gt;http://xyproblem.info/&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please explain the larger issue.&amp;nbsp; You probably just want to combine the original datasets together and then you can do the subtraction.&lt;/P&gt;</description>
      <pubDate>Mon, 20 Apr 2020 17:41:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculations-in-array-with-macro-variables/m-p/641430#M191170</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-04-20T17:41:11Z</dc:date>
    </item>
    <item>
      <title>Re: Calculations in array with macro variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculations-in-array-with-macro-variables/m-p/641435#M191171</link>
      <description>&lt;P&gt;Hi Tom,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You're right, that was me. I just wanted to ask what I needed at that time. For that that simplified my question. I got the answer I needed for both questions, but when I tried to code the second one in the macro, then I got some issues. Outside the macro it worked perfectly. I'll try to explain quickly to you the problem. I've a macro to perform some analyses. I pass some variables as a unique parameter. For example:&lt;/P&gt;
&lt;P&gt;%macro example (vars);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; data t;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; input mylib.mydata (keep = &amp;amp;vars);&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;%mend;&lt;/P&gt;
&lt;P&gt;%example (vars= a b c);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;At some point, I need to calculate the difference between some variables and those in the parameter (i.e. &amp;amp;vars) in a data set. Since I don't always pass the same number of variables, I assign to macro variables in the code that value (i.e.&amp;nbsp;&amp;amp;number_nutr). Then I want to do the following:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; array nt {&amp;amp;number_nutr.}&amp;nbsp; &amp;nbsp; &amp;amp;nutrients;&lt;BR /&gt;&amp;nbsp; array diff {&amp;amp;number_nutr.}&amp;nbsp; diff1-diff&amp;amp;number_nutr;&lt;BR /&gt;&amp;nbsp; array&amp;nbsp; v {&amp;amp;number_nutr.} _temporary_ (&amp;amp;value1 - &amp;amp;&amp;amp;value&amp;amp;number_nutr);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%do i = 1 %to &amp;amp;number_nutr.; &lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;diff{i} = nt{i}- v(i);&lt;BR /&gt;%end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The - sign in the 'V' array between and &amp;amp;&amp;amp;value&amp;amp;number_nutr is not appropiate to use. I hope this is more clear.&lt;/P&gt;
&lt;P&gt;Thanks a lot,&lt;/P&gt;
&lt;P&gt;AG.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 20 Apr 2020 17:54:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculations-in-array-with-macro-variables/m-p/641435#M191171</guid>
      <dc:creator>alexgonzalez</dc:creator>
      <dc:date>2020-04-20T17:54:29Z</dc:date>
    </item>
    <item>
      <title>Re: Calculations in array with macro variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculations-in-array-with-macro-variables/m-p/641436#M191172</link>
      <description>&lt;P&gt;You are still skipping some large step in the middle of your explanation.&amp;nbsp; You say you start with a list of VARIABLE names.&amp;nbsp; Then some how you start talking about MACRO VARIABLES.&amp;nbsp; When did you make the jump from variables to macro variables? Why did you make the jump? Are they supposed to be the same in some sense?&amp;nbsp; If so how are the two related.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you have a list of VARIABLE names in the macro variable NURIENTS.&amp;nbsp; And a second list of WHAT? (offset values?) in a second dataset that is using the variables listed in the macro variable VARS then it looks like you want to write this code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  if _n_=1 then set offsets(keep=&amp;amp;vars);
  array A &amp;amp;nutrients;
  array B &amp;amp;vars;
  array diff [%sysfunc(countw(&amp;amp;nutrients,%str( )))];
  do i=1 to dim(a);
   diff[i]=a[i]-b[i];
  end;
  drop i &amp;amp;vars;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The only thing that needs to know the number of variables is the definition of the size of the array that is creating the NEW variables.&amp;nbsp; But you can just count how many you have.&amp;nbsp; (Plus the two macro variables have to have the same number of variables listed.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 20 Apr 2020 18:04:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculations-in-array-with-macro-variables/m-p/641436#M191172</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-04-20T18:04:30Z</dc:date>
    </item>
    <item>
      <title>Re: Calculations in array with macro variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculations-in-array-with-macro-variables/m-p/641440#M191175</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/152501"&gt;@alexgonzalez&lt;/a&gt;&amp;nbsp;How are you creating this macro variables instead? I suspect changing that would be easier.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
select distinct age into :age_list separated by " "
from sashelp.class;
quit;
%let nCount = &amp;amp;sqlObs;

%put &amp;amp;age;
%put &amp;amp;ncount;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And arrays can be wild carded. I don't think that works for temporary arrays but the count should help you above. It would simplify your logic a lot.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;array myArray (*) myVar1-myVar&amp;amp;nCount;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 20 Apr 2020 18:18:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculations-in-array-with-macro-variables/m-p/641440#M191175</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-04-20T18:18:46Z</dc:date>
    </item>
    <item>
      <title>Re: Calculations in array with macro variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculations-in-array-with-macro-variables/m-p/641443#M191178</link>
      <description>&lt;P&gt;For permanent arrays you don't need to specify the number of elements. But for temporary arrays you MUST.&lt;/P&gt;
&lt;P&gt;Use the nCount variable your code creates.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;array age_list [&amp;amp;nCount] _temporary_  (&amp;amp;age_list) ;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 20 Apr 2020 18:26:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculations-in-array-with-macro-variables/m-p/641443#M191178</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-04-20T18:26:11Z</dc:date>
    </item>
    <item>
      <title>Re: Calculations in array with macro variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculations-in-array-with-macro-variables/m-p/641648#M191282</link>
      <description>&lt;P&gt;Hi Tom,&lt;/P&gt;
&lt;P&gt;First of all, thanks a lot for taking the time for this, very much appreciated. I’ll provide you with all the information you need. Just want to mention that I simplified the macro for you, and change variable names and datasets for simplification purpose. So it is probably (not very, I hope so) that if you run this macro you could probably get some error.&lt;/P&gt;
&lt;P&gt;Let’s say I’ve one object with multiple characteristics (variables), and I want to compare it with other objects on the same characteristics using the Euclidean distance.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%macro example (vars);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;data one;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; input mylib.one (keep = id &amp;amp;vars); /*One record only*/&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data many;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; input mylib.many (keep = id &amp;amp;vars); /*Multiple records*/&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;*Counting the # of specified variables in the ‘vars’ parameter and creating a var list;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; proc transpose data=one out=z (drop=_label_ );&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; var &amp;amp;var;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; proc sql noprint;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; select distinct _name_ into :var_list separated by " "&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; from z;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; %let number_vars = &amp;amp;sqlObs;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; *Assigning macro values to each variable in the &amp;amp;vars parameter;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; data _null_ ;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; set z ;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; call symputx(cats("value",_n_),col1) ;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; *Preparing dataset the calculation of the Euclidean distance;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; data all;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; set one many;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;*Calculating the Euclidean distance;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; proc distance data=all out=Dist1 (keep= dist1) method=Euclid;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; var interval(&amp;amp;vars);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; data dist2;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; set dist1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if _n_=1 then delete;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rename dist1 = d;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;*Matching each object with a distance value;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;data many_dist;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;merge many &amp;nbsp;dist2;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; *Renaming &amp;amp;vars before merging back with ‘One’;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;%do i = 1 %to &amp;amp;number_vars.;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %scan(&amp;amp;vars.,&amp;amp;i.) = key_&amp;amp;i.;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %end;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; *Getting the final dataset;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;*This dataset will contain in the first records and 1+&amp;amp; number_vars columns,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;the information from ‘One’. The subsequent columns (and as many records&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; as in the dataset ‘many’ the information from many_dist.;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; data final&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; merge on many_dist;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; *Here is where I want to calculate the difference between&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;amp;value1-key_1, &amp;amp;value2-key_2 … &amp;amp;valueK-key_K using arrays.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;As you can notice the values &amp;amp;value1 to &amp;amp;valueK are the same that appear&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; in the second to +&amp;amp; number_vars columns, but only in the first row.;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;%mend;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%example (vars= a b c);&amp;nbsp; *The # &amp;nbsp;of variables in the ‘vars’ parameter can change;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I hope this is clear enough. Looking forward to getting your inputs.&lt;/P&gt;
&lt;P&gt;Thanks again,&lt;/P&gt;
&lt;P&gt;AG.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 21 Apr 2020 15:38:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculations-in-array-with-macro-variables/m-p/641648#M191282</guid>
      <dc:creator>alexgonzalez</dc:creator>
      <dc:date>2020-04-21T15:38:11Z</dc:date>
    </item>
    <item>
      <title>Re: Calculations in array with macro variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculations-in-array-with-macro-variables/m-p/641759#M191334</link>
      <description>&lt;P&gt;I would suggest providing a small example of a starting data set and what the result should be.&lt;/P&gt;
&lt;P&gt;Since you have not provided an actual example data set we can't test your code (if this is supposed to work) to see what a result should be.&lt;/P&gt;
&lt;P&gt;I get a feeling that there is a lot of complication involved.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And maybe belongs in Proc IML. Maybe.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 21 Apr 2020 21:57:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculations-in-array-with-macro-variables/m-p/641759#M191334</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-04-21T21:57:19Z</dc:date>
    </item>
    <item>
      <title>Re: Calculations in array with macro variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculations-in-array-with-macro-variables/m-p/641992#M191457</link>
      <description>&lt;P&gt;That's a great suggestion Ballardw. I should have though about it, appologie. See below everything you need. The code runs perfectly.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a; 
input id $ a b c d e;
datalines; 
001 100 70 85 86 94 
;

data b; 
input id $ a b c;
datalines; 
002 87 78 60 
003 99 45 77
004 103 95 73 
;


%macro example (vars);

data one;
set a (keep = id &amp;amp;vars); /*One record only*/
run;

data many;
set b (keep = id &amp;amp;vars); /*Multiple records*/
run;

*Counting the # of specified variables in the ‘vars’ parameter and creating a var list;

proc transpose data=one out=z;
var &amp;amp;vars;
run;

proc sql noprint;
select distinct _name_ into :var_list separated by " "
from z;
quit;
%let number_vars = &amp;amp;sqlObs;

*Assigning macro values to each variable in the &amp;amp;vars parameter;

data _null_ ;
set z ;
call symputx(cats("value",_n_),col1) ;
run;

*Preparing dataset the calculation of the Euclidean distance;

data all;
set one many;
run;

*Calculating the Euclidean distance;

proc distance data=all out=Dist (keep= dist1) method=Euclid;
var interval(&amp;amp;vars);
run;

data dist;
set dist;
if _n_=1 then delete;
rename dist1 = d;
run;

*Matching each object with a distance value;

data many_dist;
merge many dist;

*Renaming &amp;amp;vars before merging back with ‘One’;
rename id = idcode;

%do i = 1 %to &amp;amp;number_vars.;

rename %scan(&amp;amp;vars.,&amp;amp;i.) = key_&amp;amp;i.;

%end; 
run;

*Getting the final dataset;

*This dataset will contain in the first records and 1+&amp;amp; number_vars columns,
the information from ‘One’. The subsequent columns (and as many records
as in the dataset ‘many’ the information from many_dist.;

data final;
merge one many_dist;

/*Here is where I want to calculate the difference between
&amp;amp;value1-key_1, &amp;amp;value2-key_2 … &amp;amp;valueK-key_K using arrays.

As you can notice the values &amp;amp;value1 to &amp;amp;valueK are the same that appear
in the second to +&amp;amp; number_vars columns, but only in the first row.;*/
run;

proc datasets lib=work;
delete one many: all z dist;
run;

%mend;

%example (vars= a b c)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the dataset 'final'&amp;nbsp; I want to calculate three differences (maybe more depending on the # of variables in the &amp;amp;vars parameter), key_1-a, key_2-b, and key_3-c for each object&amp;nbsp;002,003, and&amp;nbsp;004. I hope this time I totally clear.&lt;/P&gt;
&lt;P&gt;Thank you very much. A.G.&lt;/P&gt;</description>
      <pubDate>Wed, 22 Apr 2020 16:00:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculations-in-array-with-macro-variables/m-p/641992#M191457</guid>
      <dc:creator>alexgonzalez</dc:creator>
      <dc:date>2020-04-22T16:00:15Z</dc:date>
    </item>
    <item>
      <title>Re: Calculations in array with macro variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculations-in-array-with-macro-variables/m-p/642006#M191463</link>
      <description>Post your code using the code button. It makes it easier to read and follow and prevents issues when copying and pasting your code from HTML to an editor.</description>
      <pubDate>Wed, 22 Apr 2020 15:59:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculations-in-array-with-macro-variables/m-p/642006#M191463</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-04-22T15:59:41Z</dc:date>
    </item>
    <item>
      <title>Re: Calculations in array with macro variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculations-in-array-with-macro-variables/m-p/642011#M191465</link>
      <description>&lt;P&gt;What is it that you want out of that?&amp;nbsp; Why are you doing all of that crazy macro stuff?&lt;/P&gt;
&lt;P&gt;&lt;A href="https://forum.wordreference.com/threads/idiom-for-doing-something-simple-in-a-lengthier-way.3366230/" target="_blank" rel="noopener"&gt;https://forum.wordreference.com/threads/idiom-for-doing-something-simple-in-a-lengthier-way.3366230/&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let vars=a b c;

data all;
  set a b;
  keep &amp;amp;vars;
run;

proc distance data=all out=Dist(keep=Dist1 rename=Dist1=d) method=Euclid;
  var interval(&amp;amp;vars);
run;

data want;
  set b;
  set dist(firstobs=2) ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;Obs     a      b     c       d

 1      87    78    60    29.2916
 2      99    45    77    26.2679
 3     103    95    73    27.8927&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 22 Apr 2020 16:51:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculations-in-array-with-macro-variables/m-p/642011#M191465</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-04-22T16:51:03Z</dc:date>
    </item>
    <item>
      <title>Re: Calculations in array with macro variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculations-in-array-with-macro-variables/m-p/642012#M191466</link>
      <description>&lt;P&gt;I definitely don't quite understand what you're trying to do overall, not sure why SET doesn't work as well? I feel like this could simplify your problem. However, assuming you want to stick with the approach identified, see a simplified solution below.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This would give the same solution as below - no macro logic besides the names and number of them needed which is trivial to get.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data final;
if _n_ =1 then set one;
set many_dist;


array diff(&amp;amp;number_vars) diff1-diff3;
array key(&amp;amp;number_vars) key:;
array _refs(&amp;amp;number_vars) &amp;amp;vars;

do i=1 to &amp;amp;number_vars.;
diff(i) = key(i) - _refs(i);
end;


run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Some notes:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Your &amp;amp;vars and var_list seem to be the same macro variable so not sure why you need to recreate it.&amp;nbsp;&lt;/LI&gt;
&lt;LI&gt;Creating the macro variables can be simplified massively, which I use here within the same data step as 'One'&lt;/LI&gt;
&lt;LI&gt;Rather than create multiple macro variables, I create a single one to use with all values in it.&lt;/LI&gt;
&lt;/OL&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a;
	input id $ a b c d e;
	datalines;
001 100 70 85 86 94 
;

data b;
	input id $ a b c;
	datalines;
002 87 78 60 
003 99 45 77
004 103 95 73 
;

	%macro example (vars);
		%let number_vars = %sysfunc(countw(&amp;amp;vars)) ;

	data one;
		set a (keep=id &amp;amp;vars);

		/*One record only*/
		array _myvars(&amp;amp;number_vars) &amp;amp;vars.;
		call symputx('value_list', catx(' ', of _myvars(*)));
	run;

	data many;
		set b (keep=id &amp;amp;vars);

		/*Multiple records*/
	run;

	*Preparing dataset the calculation of the Euclidean distance;

	data all;
		set one many;
	run;

	*Calculating the Euclidean distance;

	proc distance data=all out=Dist (keep=dist1) method=Euclid;
		var interval(&amp;amp;vars);
	run;

	data dist;
		set dist;

		if _n_=1 then
			delete;
		rename dist1=d;
	run;

	*Matching each object with a distance value;

	data many_dist;
		merge many dist;
		*Renaming &amp;amp;vars before merging back with ‘One’;
		rename id=idcode;

		%do i=1 %to &amp;amp;number_vars.;
			rename %scan(&amp;amp;vars., &amp;amp;i.)=key_&amp;amp;i.;
		%end;
	run;

	data final;
		merge one many_dist;
		array diff(&amp;amp;number_vars) diff1-diff3;
		array key(&amp;amp;number_vars) key:;
		array _refs(&amp;amp;number_vars) _temporary_ (&amp;amp;value_list);

		do i=1 to &amp;amp;number_vars.;
			diff(i)=key(i) - _refs(i);
		end;
	run;

	data final2;
		if _n_=1 then
			set one;
		set many_dist;
		array diff(&amp;amp;number_vars) diff1-diff3;
		array key(&amp;amp;number_vars) key:;
		array _refs(&amp;amp;number_vars) &amp;amp;vars;

		do i=1 to &amp;amp;number_vars.;
			diff(i)=key(i) - _refs(i);
		end;
	run;

	proc datasets lib=work;
		delete one many: all z dist;
		run;
	%mend;

	%example (vars=a b c)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 22 Apr 2020 16:52:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculations-in-array-with-macro-variables/m-p/642012#M191466</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-04-22T16:52:38Z</dc:date>
    </item>
    <item>
      <title>Re: Calculations in array with macro variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculations-in-array-with-macro-variables/m-p/642015#M191469</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;	%macro example (vars);
		%let number_vars = %sysfunc(countw(&amp;amp;vars)) ;



	*Preparing dataset the calculation of the Euclidean distance;

	data all;
		set a (keep=id &amp;amp;vars)  b (keep=id &amp;amp;vars);
	run;

	*Calculating the Euclidean distance;

	proc distance data=all out=Dist (keep=dist1) method=Euclid;
		var interval(&amp;amp;vars);
	run;

	data dist;
		set dist;

		if _n_=1 then
			delete;
		rename dist1=d;
	run;

	*Matching each object with a distance value;

	data many_dist;
		merge b (keep = id &amp;amp;vars) dist;
		*Renaming &amp;amp;vars before merging back with ‘One’;
		rename id=idcode;

		%do i=1 %to &amp;amp;number_vars.;
			rename %scan(&amp;amp;vars., &amp;amp;i.)=key_&amp;amp;i.;
		%end;
	run;



	data final;
		if _n_=1 then
			set a (keep = id &amp;amp;vars);
			
		set many_dist;
		array diff(*) diff1-diff&amp;amp;number_vars;
		array key(*) key:;
		array _refs(*) &amp;amp;vars;

		do i=1 to dim(diff);
			diff(i)=key(i) - _refs(i);
		end;
	run;

	proc datasets lib=work;
		delete  many: all  dist;
		run;
	%mend;

	%example (vars=a b c)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This is the full simplified version that matches the results.&lt;/P&gt;</description>
      <pubDate>Wed, 22 Apr 2020 16:58:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculations-in-array-with-macro-variables/m-p/642015#M191469</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-04-22T16:58:03Z</dc:date>
    </item>
    <item>
      <title>Re: Calculations in array with macro variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculations-in-array-with-macro-variables/m-p/642058#M191494</link>
      <description>That worked Reeza. Thanks a lot for your great suggestions. I modified my macro it's much better now. I tried the code in the 'final'  data set (with the other suggestions in the code). Very much appreciated.</description>
      <pubDate>Wed, 22 Apr 2020 18:52:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculations-in-array-with-macro-variables/m-p/642058#M191494</guid>
      <dc:creator>alexgonzalez</dc:creator>
      <dc:date>2020-04-22T18:52:27Z</dc:date>
    </item>
  </channel>
</rss>

