<?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: How to create array in if statement in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-array-in-if-statement/m-p/785455#M250668</link>
    <description>&lt;P&gt;If the weight depends on the ID then why not just put them into another dataset instead of storing the weights in the code?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input xyz_id letter $ Col_1 col_2;
cards;
1 Z 2 1
2 Z 3 2
3 Z 4 3
;

data weights;
  input xyz_id weight1-weight3 ;
cards;
1 1 2 3
2 8 7 6
3 1 1 1
;

data want;
  merge have weights ;
  by xyz_id;
  array weights weight1-weight3 ;
  array letters [3] $3 _temporary_ ('A' 'B' 'C');
  array old col_1-col_2;
  array new new_1-new_2;
  do i=1 to dim(letters);
    letter=letters[i];
    do j=1 to dim(old);
      new[j] = old[j] * weights[i];
    end;
    output;
  end;
run;

proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;PS It is just as easy to type the data step to create the data as it is to type those tables with the |'s in them.&amp;nbsp; And if you use the Insert SAS Code icon so you get a fixed font it is just as easy to read. The |'s just limit the amount of information you can put on a line.&lt;/P&gt;</description>
    <pubDate>Fri, 10 Dec 2021 19:10:36 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2021-12-10T19:10:36Z</dc:date>
    <item>
      <title>How to create array in if statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-array-in-if-statement/m-p/785339#M250625</link>
      <description>&lt;P&gt;I'd like to replicate and modify specific rows in the table.&lt;/P&gt;&lt;P&gt;before:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;xyz_id | letter | Col_1 | ...|
1 | Z | V1 | ... |
2 | Z | V2 | ... |
3 | Z | V3 | ... |&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;after:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;xyz_id | letter | Col_1 | ...|
1 | A | V1.1 | ... |
1 | B | V1.2 | ... |
1 | C | V1.3 | ... |
2 | A | V2.1 | ... |
2 | B | V2.2 | ... |
2 | C | V2.3 | ... |
3 | A | V3.1 | ... |
3 | B | V3.2 | ... |
3 | C | V3.3 | ... |&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I've prepared the following code:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;data test2;
        set test;

        
        array letters {3} $3 _temporary_ ('A', 'B', 'C');
        array weights {3}  _temporary_ (1,2,3);

/*      if xyz_id = '1' */
/*          then  array weights {3}  _temporary_ (1,2,3);*/
/*      else if xyz_id = '2'*/
/*          then array weights {3}  _temporary_ (8,7,6);*/
/*      else array weights {3}  _temporary_ (1,1,1)*/


        do i = 1 to 8;
            letter = letters(i);
            Col_A = Col_A * weights(i);
            output;
        end;
          drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Now, I'm trying to make&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;EM&gt;weights&lt;/EM&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;depend of the&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;EM&gt;letter&lt;/EM&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;column (commented code) - but without any success. I also tried:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;   array weights_1 {3}  _temporary_ (1,2,3);
   if xyz_id = '1'
            then  weights = weights_1;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;but it didn't work as well.&lt;/P&gt;&lt;P&gt;Any suggestions?&lt;/P&gt;</description>
      <pubDate>Fri, 10 Dec 2021 09:40:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-array-in-if-statement/m-p/785339#M250625</guid>
      <dc:creator>ToRsy</dc:creator>
      <dc:date>2021-12-10T09:40:20Z</dc:date>
    </item>
    <item>
      <title>Re: How to create array in if statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-array-in-if-statement/m-p/785343#M250629</link>
      <description>&lt;P&gt;An array is a static object defined once and created during the data step compilation phase. You cannot create it conditionally (unless you generate the&amp;nbsp;&lt;EM&gt;code&lt;/EM&gt; conditionally with macro language).&lt;/P&gt;
&lt;P&gt;You can create a large array with all weights, and then set the&amp;nbsp;&lt;EM&gt;index&lt;/EM&gt; conditionally depending on a variable value.&lt;/P&gt;</description>
      <pubDate>Fri, 10 Dec 2021 09:56:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-array-in-if-statement/m-p/785343#M250629</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-12-10T09:56:40Z</dc:date>
    </item>
    <item>
      <title>Re: How to create array in if statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-array-in-if-statement/m-p/785455#M250668</link>
      <description>&lt;P&gt;If the weight depends on the ID then why not just put them into another dataset instead of storing the weights in the code?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input xyz_id letter $ Col_1 col_2;
cards;
1 Z 2 1
2 Z 3 2
3 Z 4 3
;

data weights;
  input xyz_id weight1-weight3 ;
cards;
1 1 2 3
2 8 7 6
3 1 1 1
;

data want;
  merge have weights ;
  by xyz_id;
  array weights weight1-weight3 ;
  array letters [3] $3 _temporary_ ('A' 'B' 'C');
  array old col_1-col_2;
  array new new_1-new_2;
  do i=1 to dim(letters);
    letter=letters[i];
    do j=1 to dim(old);
      new[j] = old[j] * weights[i];
    end;
    output;
  end;
run;

proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;PS It is just as easy to type the data step to create the data as it is to type those tables with the |'s in them.&amp;nbsp; And if you use the Insert SAS Code icon so you get a fixed font it is just as easy to read. The |'s just limit the amount of information you can put on a line.&lt;/P&gt;</description>
      <pubDate>Fri, 10 Dec 2021 19:10:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-array-in-if-statement/m-p/785455#M250668</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-12-10T19:10:36Z</dc:date>
    </item>
  </channel>
</rss>

