<?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: Assign a value from a table to a macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Assign-a-value-from-a-table-to-a-macro/m-p/894894#M353494</link>
    <description>&lt;P&gt;You don't need macro code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set have;

   array vars[3] var1_unit1 var2_unit2 var3_unit3;

   if ans = 'yes' then Colour = vars[pos];
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 19 Sep 2023 08:30:27 GMT</pubDate>
    <dc:creator>andreas_lds</dc:creator>
    <dc:date>2023-09-19T08:30:27Z</dc:date>
    <item>
      <title>Assign a value from a table to a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assign-a-value-from-a-table-to-a-macro/m-p/894891#M353493</link>
      <description>&lt;P&gt;Dear all,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;if I have a data that looks like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	infile datalines;
	input ans $4. pos var1_unit1 $6.var2_unit2 $7. var3_unit3 $6.;
	datalines;
yes 1 yellow red	black
no  2 blue   white  braun
no  2 Orange green	pink 
yes 3 green	 blue   gray
yes 1 white	 purple green
;
run;

data _null_;
set have;
call symput("M", pos); 
run;

%put my value is; &amp;amp;M;

data want;
set have;
if ans="yes" then color=var&amp;amp;M._unit;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I want to output the value of color using the&amp;nbsp; "pos" value if the condition =yes&lt;/P&gt;
&lt;P&gt;but am not getting it. please any help?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My result should look like this&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data result;
	infile datalines;
	input ans $4. pos var1_unit1 $6.var2_unit2 $7. var3_unit3 $6. color $7.;
	datalines;
yes 1 yellow red	black yellow
no  2 blue   white  braun
no  2 Orange green	pink
yes 3 green	 blue   gray  gray
yes 1 white	 purple green white
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 19 Sep 2023 08:21:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assign-a-value-from-a-table-to-a-macro/m-p/894891#M353493</guid>
      <dc:creator>Anita_n</dc:creator>
      <dc:date>2023-09-19T08:21:21Z</dc:date>
    </item>
    <item>
      <title>Re: Assign a value from a table to a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assign-a-value-from-a-table-to-a-macro/m-p/894894#M353494</link>
      <description>&lt;P&gt;You don't need macro code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set have;

   array vars[3] var1_unit1 var2_unit2 var3_unit3;

   if ans = 'yes' then Colour = vars[pos];
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 19 Sep 2023 08:30:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assign-a-value-from-a-table-to-a-macro/m-p/894894#M353494</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2023-09-19T08:30:27Z</dc:date>
    </item>
    <item>
      <title>Re: Assign a value from a table to a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assign-a-value-from-a-table-to-a-macro/m-p/894896#M353496</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15475"&gt;@andreas_lds&lt;/a&gt;&amp;nbsp;I get this error message running the code in the second part of the program, Any idea&amp;nbsp; how to solve this. In the first part it worked&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;ERROR: Array subscript out of range in row15 column 97.&lt;/P&gt;</description>
      <pubDate>Tue, 19 Sep 2023 09:09:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assign-a-value-from-a-table-to-a-macro/m-p/894896#M353496</guid>
      <dc:creator>Anita_n</dc:creator>
      <dc:date>2023-09-19T09:09:29Z</dc:date>
    </item>
    <item>
      <title>Re: Assign a value from a table to a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assign-a-value-from-a-table-to-a-macro/m-p/894897#M353497</link>
      <description>&lt;P&gt;Please post the complete log including the code.&lt;/P&gt;</description>
      <pubDate>Tue, 19 Sep 2023 09:16:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assign-a-value-from-a-table-to-a-macro/m-p/894897#M353497</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2023-09-19T09:16:27Z</dc:date>
    </item>
    <item>
      <title>Re: Assign a value from a table to a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assign-a-value-from-a-table-to-a-macro/m-p/894898#M353498</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15475"&gt;@andreas_lds&lt;/a&gt;&amp;nbsp;Is it possible to use a macro instead of the array? My code contains some patient details I can unfortunately not post it here.&lt;/P&gt;
&lt;P&gt;That was the log message I got.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 19 Sep 2023 09:30:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assign-a-value-from-a-table-to-a-macro/m-p/894898#M353498</guid>
      <dc:creator>Anita_n</dc:creator>
      <dc:date>2023-09-19T09:30:00Z</dc:date>
    </item>
    <item>
      <title>Re: Assign a value from a table to a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assign-a-value-from-a-table-to-a-macro/m-p/894899#M353499</link>
      <description>&lt;P&gt;The code from &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15475"&gt;@andreas_lds&lt;/a&gt; ought to work.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The error message&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;ERROR: Array subscript out of range in row15 column 97.&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;usually indicates that the value of the subscript (in &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15475"&gt;@andreas_lds&lt;/a&gt; code it is variable POS) is either greater than 3 (because in his code the ARRAY had 3 elements) or it is less than 1, or missing, or not an integer. So a value of pos=4, or pos=., or pos=0 (and many other values) will cause this error to happen. Only integer values 1 or 2 or 3 will work here. So you need to look at the values of POS in your actual data, and make sure they are integers between 1 and the number of variables in the array.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;No need for a macro solution here, and furthermore the macro solution will run into the same difficulties.&lt;/P&gt;</description>
      <pubDate>Tue, 19 Sep 2023 09:54:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assign-a-value-from-a-table-to-a-macro/m-p/894899#M353499</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-09-19T09:54:45Z</dc:date>
    </item>
    <item>
      <title>Re: Assign a value from a table to a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assign-a-value-from-a-table-to-a-macro/m-p/894900#M353500</link>
      <description>&lt;P&gt;Can you post the array statement, at least? If you can't, it will hardly possible to help you.&lt;/P&gt;</description>
      <pubDate>Tue, 19 Sep 2023 10:14:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assign-a-value-from-a-table-to-a-macro/m-p/894900#M353500</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2023-09-19T10:14:55Z</dc:date>
    </item>
    <item>
      <title>Re: Assign a value from a table to a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assign-a-value-from-a-table-to-a-macro/m-p/894901#M353501</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15475"&gt;@andreas_lds&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Okay let me try to explain the problem this way. I have adjusted the code to make it look like the way I have it in my main code&lt;/P&gt;
&lt;P&gt;I hope this can help&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	infile datalines;
	input ans $4. pos1 2. pos2 2. pos3 2. pos4 2. pos5 2. pos6 2.  var1_unit1 $6.var2_unit2 $7. var3_unit3 $6. 
    var4_unit4 $7.  var5_unit5 $7.  var6_unit6 $7.  var7_unit7 $6.  var8_unit8 $7.  var9_unit9 $6.  var10_unit10 $6. 
    var11_unit11 $6. var12_unit12 $7.;
	datalines;
yes 1 2 1 5 1 4 yellow red	  black yellow yellow red	black yellow yellow red	  black yellow
no  2 3 5   1 4 blue   white  braun blue   white  braun blue  white  braun  blue  white braun
no  2   4   2   Orange green  pink  blue   white  braun blue  white  braun  blue  white braun
yes 3 1   2 5   green  blue   gray  gray   Orange green pink  blue   Orange green pink  blue
yes 1 4   2 4   white  purple green white  green  blue  gray  gray   green  blue  gray  gray
;
run;

%macro want(outdat, num);
data &amp;amp;outdat;
   set have;

   array vars[12] var1_unit1 var2_unit2 var3_unit3 var4_unit4 var5_unit5 var6_unit6 var7_unit7 
          var8_unit8 var9_unit9 var10_unit10 var11_unit11 var12_unit12;

   if ans = 'yes' then Colour = vars[&amp;amp;num];
run;
%mend want;
%want(want1, pos1); %want(want2, pos2); %want(want3, pos3); %want(want4, pos4); %want(want5, pos5); %want(want6, pos6); 
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Thanks for the help&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>Tue, 19 Sep 2023 11:33:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assign-a-value-from-a-table-to-a-macro/m-p/894901#M353501</guid>
      <dc:creator>Anita_n</dc:creator>
      <dc:date>2023-09-19T11:33:05Z</dc:date>
    </item>
    <item>
      <title>Re: Assign a value from a table to a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assign-a-value-from-a-table-to-a-macro/m-p/894904#M353503</link>
      <description>&lt;P&gt;And what is wrong with this code? Please tell us. Do not simply show us code that doesn't work without explanation; explain. If it doesn't work and there are errors in the log, show us the entire LOG for this code. Since there are macros involved, please turn on macro debugging options first by running this line of code before the rest of your code, and then show us the log.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options mprint;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There is no customer data here, so please don't use that as an excuse.&lt;/P&gt;</description>
      <pubDate>Tue, 19 Sep 2023 11:45:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assign-a-value-from-a-table-to-a-macro/m-p/894904#M353503</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-09-19T11:45:21Z</dc:date>
    </item>
    <item>
      <title>Re: Assign a value from a table to a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assign-a-value-from-a-table-to-a-macro/m-p/894905#M353504</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/168930"&gt;@Anita_n&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15475"&gt;@andreas_lds&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Okay let me try to explain the problem this way. I have adjusted the code to make it look like the way I have it in my main code&lt;/P&gt;
&lt;P&gt;I hope this can help&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	infile datalines;
	input ans $4. pos1 2. pos2 2. pos3 2. pos4 2. pos5 2. pos6 2.  var1_unit1 $6.var2_unit2 $7. var3_unit3 $6. 
    var4_unit4 $7.  var5_unit5 $7.  var6_unit6 $7.  var7_unit7 $6.  var8_unit8 $7.  var9_unit9 $6.  var10_unit10 $6. 
    var11_unit11 $6. var12_unit12 $7.;
	datalines;
yes 1 2 1 5 1 4 yellow red	  black yellow yellow red	black yellow yellow red	  black yellow
no  2 3 5   1 4 blue   white  braun blue   white  braun blue  white  braun  blue  white braun
no  2   4   2   Orange green  pink  blue   white  braun blue  white  braun  blue  white braun
yes 3 1   2 5   green  blue   gray  gray   Orange green pink  blue   Orange green pink  blue
yes 1 4   2 4   white  purple green white  green  blue  gray  gray   green  blue  gray  gray
;
run;

%macro want(outdat, num);
data &amp;amp;outdat;
   set have;

   array vars[12] var1_unit1 var2_unit2 var3_unit3 var4_unit4 var5_unit5 var6_unit6 var7_unit7 
          var8_unit8 var9_unit9 var10_unit10 var11_unit11 var12_unit12;

   if ans = 'yes' then Colour = vars[&amp;amp;num];
run;
%mend want;
%want(want1, pos1); %want(want2, pos2); %want(want3, pos3); %want(want4, pos4); %want(want5, pos5); %want(want6, pos6); 
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You cannot use macro variable &amp;amp;num as the argument to an array call in this cased, because the value of macro variable &amp;amp;num is NOT an integer between 1 and 12. What is the value of &amp;amp;num?? The first time you call it, the value of &amp;amp;num is pos1, which is not an integer.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I still contend that macros are not needed here, macros just complicate things unnecessarily. Why do you feel you need to run %WANT multiple times? Please explain. Also please note, I am not asking you to explain the code, I want to understand WHY you are doing things and what the desired output is.&lt;/P&gt;</description>
      <pubDate>Tue, 19 Sep 2023 11:44:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assign-a-value-from-a-table-to-a-macro/m-p/894905#M353504</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-09-19T11:44:45Z</dc:date>
    </item>
    <item>
      <title>Re: Assign a value from a table to a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assign-a-value-from-a-table-to-a-macro/m-p/894906#M353505</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*Try function VVALUEX()*/

data have;
	infile datalines;
	input ans $4. pos1 2. pos2 2. pos3 2. pos4 2. pos5 2. pos6 2.  var1_unit1 $6.var2_unit2 $7. var3_unit3 $6. 
    var4_unit4 $7.  var5_unit5 $7.  var6_unit6 $7.  var7_unit7 $6.  var8_unit8 $7.  var9_unit9 $6.  var10_unit10 $6. 
    var11_unit11 $6. var12_unit12 $7.;
	datalines;
yes 1 2 1 5 1 4 yellow red	  black yellow yellow red	black yellow yellow red	  black yellow
no  2 3 5   1 4 blue   white  braun blue   white  braun blue  white  braun  blue  white braun
no  2   4   2   Orange green  pink  blue   white  braun blue  white  braun  blue  white braun
yes 3 1   2 5   green  blue   gray  gray   Orange green pink  blue   Orange green pink  blue
yes 1 4   2 4   white  purple green white  green  blue  gray  gray   green  blue  gray  gray
;
run;

data want;
 set have;
if not missing(pos1) then want1=vvaluex(cats('var',pos1,'_unit',pos1));
if not missing(pos2) then want2=vvaluex(cats('var',pos2,'_unit',pos2));
if not missing(pos3) then want3=vvaluex(cats('var',pos3,'_unit',pos3));
if not missing(pos4) then want4=vvaluex(cats('var',pos4,'_unit',pos4));
if not missing(pos5) then want5=vvaluex(cats('var',pos5,'_unit',pos5));
if not missing(pos6) then want6=vvaluex(cats('var',pos6,'_unit',pos6));
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 19 Sep 2023 11:47:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assign-a-value-from-a-table-to-a-macro/m-p/894906#M353505</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2023-09-19T11:47:44Z</dc:date>
    </item>
    <item>
      <title>Re: Assign a value from a table to a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assign-a-value-from-a-table-to-a-macro/m-p/894912#M353508</link>
      <description>&lt;P&gt;You do have missing values in your posX variables, which can't be used as index to an array. Your code must provide for this situation. Assign colour only when the posX variable contains a value between 1 and dim(vars).&lt;/P&gt;</description>
      <pubDate>Tue, 19 Sep 2023 13:11:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assign-a-value-from-a-table-to-a-macro/m-p/894912#M353508</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-09-19T13:11:23Z</dc:date>
    </item>
    <item>
      <title>Re: Assign a value from a table to a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assign-a-value-from-a-table-to-a-macro/m-p/894946#M353524</link>
      <description>&lt;PRE class="language-sas"&gt;&lt;CODE&gt;if not missing(pos1)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;this solved the problem. Thanks to you all for the help&lt;/P&gt;</description>
      <pubDate>Tue, 19 Sep 2023 17:25:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assign-a-value-from-a-table-to-a-macro/m-p/894946#M353524</guid>
      <dc:creator>Anita_n</dc:creator>
      <dc:date>2023-09-19T17:25:56Z</dc:date>
    </item>
    <item>
      <title>Re: Assign a value from a table to a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assign-a-value-from-a-table-to-a-macro/m-p/894947#M353525</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/168930"&gt;@Anita_n&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if pos1 in (1:3) then color = array_name[pos1] ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;this solved the problem. Thanks to you all for the help&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;For checking that the index is valid it might be better to use the IN operator.&amp;nbsp; You can use the special syntax for indication a range of integers.&amp;nbsp; That will handle the missing values but also the too large or too small or non-integer values also.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So if the size (dimension) of the array is 3 then you could use:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;array list var1-var3;
if pos1 in (1:3) then color=list[pos1];&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 19 Sep 2023 17:55:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assign-a-value-from-a-table-to-a-macro/m-p/894947#M353525</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-09-19T17:55:15Z</dc:date>
    </item>
  </channel>
</rss>

