<?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: Multiplying Column Unknown Number of Times in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Multiplying-Column-Unknown-Number-of-Times/m-p/777691#M247455</link>
    <description>Thank you so much! This worked perfectly!</description>
    <pubDate>Mon, 01 Nov 2021 14:37:08 GMT</pubDate>
    <dc:creator>anbatch</dc:creator>
    <dc:date>2021-11-01T14:37:08Z</dc:date>
    <item>
      <title>Multiplying Column Unknown Number of Times</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiplying-Column-Unknown-Number-of-Times/m-p/777409#M247317</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a SAS dataset that has a column that needs to be multiplied by other columns within the dataset to create a new column for each multiplication. The problem is, this needs to be repeatable and will have a different number of columns each time.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In my example, I have 3 numbers that I need my expected_volume column to be multiplied by. What I have:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="anbatch_3-1635539159846.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/65248i8363741D12DA03C1/image-size/medium?v=v2&amp;amp;px=400" role="button" title="anbatch_3-1635539159846.png" alt="anbatch_3-1635539159846.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I want:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="anbatch_2-1635539149717.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/65247i8E4D2217E18F5278/image-size/medium?v=v2&amp;amp;px=400" role="button" title="anbatch_2-1635539149717.png" alt="anbatch_2-1635539149717.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there any way to achieve this so that it is repeatable no matter how many value columns there are?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So far I was thinking that I could do this with a macro like below, but I am not sure how to get it to repeat for the amount of times I need it to each time.&amp;nbsp; Is this possible to do in a do loop?&lt;/P&gt;&lt;P&gt;%macro repeat(num);&lt;BR /&gt;data matrix;&lt;BR /&gt;set matrix;&lt;BR /&gt;product&amp;amp;num. = expected_volume * value&amp;amp;num.;&lt;BR /&gt;run;&lt;BR /&gt;%mend;&lt;/P&gt;&lt;P&gt;%repeat(1);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for any advice!&lt;/P&gt;</description>
      <pubDate>Fri, 29 Oct 2021 20:32:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiplying-Column-Unknown-Number-of-Times/m-p/777409#M247317</guid>
      <dc:creator>anbatch</dc:creator>
      <dc:date>2021-10-29T20:32:35Z</dc:date>
    </item>
    <item>
      <title>Re: Multiplying Column Unknown Number of Times</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiplying-Column-Unknown-Number-of-Times/m-p/777412#M247318</link>
      <description>&lt;P&gt;Wil you know the number of variables ahead of time, ie 3/5?&lt;BR /&gt;&lt;BR /&gt;Either way, an array is probably your best choice, it's just how you define the data. &lt;BR /&gt;An alternative solution is to transpose your data to a long format then it doesn't matter. Do you have other columns on this table that matter?&lt;BR /&gt;&lt;BR /&gt;EDIT: seems like you do know ahead of time. It can be generalized more if you'd like to either dynamically determine the number of columns or transpose to ignore that as an issue.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro repeat(num);
data matrix;
set matrix;

array value(&amp;amp;num);
array product(&amp;amp;num);
do i=1 to &amp;amp;num.;
product(i) = expected_volume * value(i).;
end;
run;
%mend;

%repeat(1);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 29 Oct 2021 21:47:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiplying-Column-Unknown-Number-of-Times/m-p/777412#M247318</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-10-29T21:47:43Z</dc:date>
    </item>
    <item>
      <title>Re: Multiplying Column Unknown Number of Times</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiplying-Column-Unknown-Number-of-Times/m-p/777425#M247325</link>
      <description>&lt;P&gt;Possibly another case of instead of varying numbers of Product variables there should be Productname variable with one value holding variable. You don't describe where/what the "value" or "expected_volumn" comes from but perhaps that should be in a separate data set and combined to do the needed calculations.&lt;/P&gt;</description>
      <pubDate>Fri, 29 Oct 2021 22:22:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiplying-Column-Unknown-Number-of-Times/m-p/777425#M247325</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-10-29T22:22:35Z</dc:date>
    </item>
    <item>
      <title>Re: Multiplying Column Unknown Number of Times</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiplying-Column-Unknown-Number-of-Times/m-p/777454#M247347</link>
      <description>&lt;P&gt;While arrays will work, you could instead continue on the path you started.&amp;nbsp; Here's what it could look like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro repeat(num);
   %local i;
   data products;
      set matrix;
      %do i=1 %to &amp;amp;num;
         product&amp;amp;i. = expected_volume * value&amp;amp;i.;
      %end;
   run;
%mend;

%repeat(1);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Also note, you do not have to feed the number of VALUE variables to the program.&amp;nbsp; You could let SAS find that out and use whatever is in the data.&amp;nbsp; For example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
   set matrix;
   array values {*} value: ;
   call symputx('n_values', dim(values));
   stop;
run;
%repeat (&amp;amp;n_values)&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 30 Oct 2021 03:03:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiplying-Column-Unknown-Number-of-Times/m-p/777454#M247347</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2021-10-30T03:03:19Z</dc:date>
    </item>
    <item>
      <title>Re: Multiplying Column Unknown Number of Times</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiplying-Column-Unknown-Number-of-Times/m-p/777669#M247446</link>
      <description>&lt;P&gt;Once again, an issue of bad data structure causing bad code.&lt;/P&gt;
&lt;P&gt;Transpose, and it's a breeze:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input expected_volume value1 value2 value3;
datalines;
1 1 2 3
2 2 3 4
;

proc transpose
  data=have
  out=long (rename=(col1=value))
;
by expected_volume;
var value:;
run;

data want;
set long;
product = expected_volume * value;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Maxim 19 : Long Beats Wide.&lt;/P&gt;</description>
      <pubDate>Mon, 01 Nov 2021 13:19:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiplying-Column-Unknown-Number-of-Times/m-p/777669#M247446</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-11-01T13:19:18Z</dc:date>
    </item>
    <item>
      <title>Re: Multiplying Column Unknown Number of Times</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiplying-Column-Unknown-Number-of-Times/m-p/777691#M247455</link>
      <description>Thank you so much! This worked perfectly!</description>
      <pubDate>Mon, 01 Nov 2021 14:37:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiplying-Column-Unknown-Number-of-Times/m-p/777691#M247455</guid>
      <dc:creator>anbatch</dc:creator>
      <dc:date>2021-11-01T14:37:08Z</dc:date>
    </item>
    <item>
      <title>Re: Multiplying Column Unknown Number of Times</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiplying-Column-Unknown-Number-of-Times/m-p/777692#M247456</link>
      <description>Thank you! This was very helpful.</description>
      <pubDate>Mon, 01 Nov 2021 14:37:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiplying-Column-Unknown-Number-of-Times/m-p/777692#M247456</guid>
      <dc:creator>anbatch</dc:creator>
      <dc:date>2021-11-01T14:37:59Z</dc:date>
    </item>
  </channel>
</rss>

