<?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: Multiply rows in one dataset by a column in another dataset without any common variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Multiply-rows-in-one-dataset-by-a-column-in-another-dataset/m-p/532260#M145841</link>
    <description>&lt;P&gt;I know there are already several good solutions here, but there's a technique that should not be overlooked.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename recode 'path to some file';

data _null_;
infile datalines;
input name $ fix;
file recode;
put name '=' name '*' fix ';' ;
datalines;
sam 0
david 3
karen 4 
mike 8
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;That creates a file holding all the recoding equations.&amp;nbsp; Then:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
infile datalines;
input sam david karen mike;
%include recode;
datalines;
5 7 8 4
6 4 8 9
7 3 2 6
;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sat, 02 Feb 2019 04:20:15 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2019-02-02T04:20:15Z</dc:date>
    <item>
      <title>Multiply rows in one dataset by a column in another dataset without any common variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiply-rows-in-one-dataset-by-a-column-in-another-dataset/m-p/532099#M145762</link>
      <description>&lt;P&gt;Windows 10, Version 9.4&lt;/P&gt;&lt;P&gt;Good day,&lt;/P&gt;&lt;P&gt;The following code re-creates dataset beta whereby each value is multiplied by the corresponding column value in variable "fix" of dataset alpha:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.alpha;
                infile datalines;
				length nameo $5.;
                input nameo $ fix;
                datalines;
             sam 0
			 david 3
			 karen 4
			 mike 8
               ;
               run;

data work.beta;
                infile datalines;
                input sam david karen mike;
                datalines;
             5 7 8 4
			 6 4 8 9
			 7 3 2 6
               ;
run;

options symbolgen;
%let NumComp=5;
proc sql noprint;
select distinct fix into :fixit1-:fixit&amp;amp;NumComp 
from work.alpha
;
quit; 

data work.want;
	set beta;
	sam=sam*&amp;amp;fixit1;
	david=david*&amp;amp;fixit2;
	karen=karen*&amp;amp;fixit3;
	mike=mike*&amp;amp;fixit4;
run;

proc print data=work.want; title 'want final'; run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The code works and produces the desired results.&amp;nbsp; However, the actual program is longer and I would like to be able to loop through "fixit1","fixit2",...,"fixit#"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any suggestions are greatly appreciated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Yours, Jane&lt;/P&gt;</description>
      <pubDate>Fri, 01 Feb 2019 16:59:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiply-rows-in-one-dataset-by-a-column-in-another-dataset/m-p/532099#M145762</guid>
      <dc:creator>jawhitmire</dc:creator>
      <dc:date>2019-02-01T16:59:57Z</dc:date>
    </item>
    <item>
      <title>Re: Multiply rows in one dataset by a column in another dataset without any common variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiply-rows-in-one-dataset-by-a-column-in-another-dataset/m-p/532114#M145765</link>
      <description>&lt;P&gt;When you say the actual program is longer, I assume that means you have many more names.&amp;nbsp; A safer way to go would be to change the names of the macro variables you compute.&amp;nbsp; Instead of creating &amp;amp;FIXIT1, for example, create &amp;amp;SAM.&amp;nbsp; That's easily done in a DATA step:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token procnames"&gt;data&lt;/SPAN&gt; _null_&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
                &lt;SPAN class="token statement"&gt;infile&lt;/SPAN&gt; datalines&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
				&lt;SPAN class="token function"&gt;length&lt;/SPAN&gt; nameo &lt;SPAN class="token punctuation"&gt;$&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;5&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
                &lt;SPAN class="token keyword"&gt;input&lt;/SPAN&gt; nameo &lt;SPAN class="token punctuation"&gt;$&lt;/SPAN&gt; fix&lt;SPAN class="token punctuation"&gt;;&lt;BR /&gt;&lt;/SPAN&gt;call symputx(nameo, fix);
&lt;SPAN class="token datalines"&gt;                &lt;SPAN class="token keyword"&gt;datalines&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;SPAN class="token data string"&gt;
             sam 0
			 david 3
			 karen 4
			 mike 8
               &lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/SPAN&gt;
               &lt;SPAN class="token procnames"&gt;run&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now you don't need to worry about the order of the names being the same in both data sets.&amp;nbsp; The idea is to replace these statements:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;
	sam&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;sam&lt;SPAN class="token operator"&gt;*&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;fixit1&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
	david&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;david&lt;SPAN class="token operator"&gt;*&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;fixit2&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
	karen&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;karen&lt;SPAN class="token operator"&gt;*&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;fixit3&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
	mike&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;mike&lt;SPAN class="token operator"&gt;*&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;fixit4&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Instead, you would use the equivalent of:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;
	sam&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;sam&lt;SPAN class="token operator"&gt;*&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;&amp;amp;sam&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
	david&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;david&lt;SPAN class="token operator"&gt;*&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;&amp;amp;david&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
	karen&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;karen&lt;SPAN class="token operator"&gt;*&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;&amp;amp;karen&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
	mike&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;mike&lt;SPAN class="token operator"&gt;*&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;&amp;amp;mike&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There are a few ways to generate that equivalent code.&amp;nbsp; But let's take that as a starting point and see if this approach makes sense from your point of view.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 01 Feb 2019 17:21:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiply-rows-in-one-dataset-by-a-column-in-another-dataset/m-p/532114#M145765</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-02-01T17:21:47Z</dc:date>
    </item>
    <item>
      <title>Re: Multiply rows in one dataset by a column in another dataset without any common variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiply-rows-in-one-dataset-by-a-column-in-another-dataset/m-p/532115#M145766</link>
      <description>&lt;P&gt;PROC IML will do this type of multiplication easily.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, if you don't have PROC IML, you could turn the row in to a column via PROC TRANSPOSE, combine that new data set with the data set that has the column (now your data is in two columns in the same data set) and then multiplication is easy.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In either case, as long as the ordering of the values in the row is the same as the ordering of the values in the column, then you don't need to type out the names.&lt;/P&gt;</description>
      <pubDate>Fri, 01 Feb 2019 17:39:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiply-rows-in-one-dataset-by-a-column-in-another-dataset/m-p/532115#M145766</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-02-01T17:39:02Z</dc:date>
    </item>
    <item>
      <title>Re: Multiply rows in one dataset by a column in another dataset without any common variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiply-rows-in-one-dataset-by-a-column-in-another-dataset/m-p/532120#M145768</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.alpha;
                infile datalines;
				length nameo $5.;
                input nameo $ fix;
                datalines;
             sam 0
			 david 3
			 karen 4
			 mike 8
               ;
               run;

data work.beta;
                infile datalines;
                input sam david karen mike;
                datalines;
             5 7 8 4
			 6 4 8 9
			 7 3 2 6
               ;
run;

proc transpose data=alpha out=_a prefix=fixit_;
var fix;
id nameo;
run;

data want_beta;
set beta;
if _n_=1 then set _a;
array t(*)sam--mike;
array j(*) fixit:;
do i=1 to dim(t);
t(i)=t(i)*j(i);
end;
drop fixit:;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 01 Feb 2019 17:39:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiply-rows-in-one-dataset-by-a-column-in-another-dataset/m-p/532120#M145768</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-02-01T17:39:09Z</dc:date>
    </item>
    <item>
      <title>Re: Multiply rows in one dataset by a column in another dataset without any common variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiply-rows-in-one-dataset-by-a-column-in-another-dataset/m-p/532122#M145769</link>
      <description>&lt;P&gt;You should be able to do this with arrays.&amp;nbsp; Use PROC SQL to get the list of names and values into space delimited lists.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data alpha;
  input nameo :$32. fix @@;
datalines;
sam 0 david 3 karen 4 mike 8
;

data beta;
  input sam david karen mike @@;
datalines;
5 7 8 4 6 4 8 9 7 3 2 6
;

proc sql noprint;
select nameo,fix
  into :vars separated by ' '
     , :values separated by ' '
  from alpha
;
%let n=&amp;amp;sqlobs;
quit;

data want ;
  set beta ;
  array vars &amp;amp;vars ;
  array fix (&amp;amp;n) _temporary_ (&amp;amp;values);
  do _n_=1 to &amp;amp;n ;
    vars(_n_) = vars(_n_)*fix(_n_);
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;Obs    sam    david    karen    mike

 1      0       21       32      32
 2      0       12       32      72
 3      0        9        8      48&lt;/PRE&gt;</description>
      <pubDate>Fri, 01 Feb 2019 17:42:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiply-rows-in-one-dataset-by-a-column-in-another-dataset/m-p/532122#M145769</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-02-01T17:42:00Z</dc:date>
    </item>
    <item>
      <title>Re: Multiply rows in one dataset by a column in another dataset without any common variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiply-rows-in-one-dataset-by-a-column-in-another-dataset/m-p/532143#M145779</link>
      <description>&lt;P&gt;Paige&lt;/P&gt;&lt;P&gt;Thank you for the quick reply.&lt;/P&gt;&lt;P&gt;Unfortunately, "proc IML" is not available.&lt;/P&gt;&lt;P&gt;A transpose would definitely help to merge the datasets.&lt;/P&gt;&lt;P&gt;Thanks again,&lt;/P&gt;&lt;P&gt;Jane&lt;/P&gt;</description>
      <pubDate>Fri, 01 Feb 2019 18:24:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiply-rows-in-one-dataset-by-a-column-in-another-dataset/m-p/532143#M145779</guid>
      <dc:creator>jawhitmire</dc:creator>
      <dc:date>2019-02-01T18:24:19Z</dc:date>
    </item>
    <item>
      <title>Re: Multiply rows in one dataset by a column in another dataset without any common variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiply-rows-in-one-dataset-by-a-column-in-another-dataset/m-p/532148#M145780</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/248885"&gt;@jawhitmire&lt;/a&gt;&amp;nbsp; Hash &lt;STRONG&gt;&lt;EM&gt;might&lt;/EM&gt; &lt;/STRONG&gt;make it a touch faster&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.alpha;
                infile datalines;
				length nameo $5.;
                input nameo $ fix;
                datalines;
             sam 0
			 david 3
			 karen 4
			 mike 8
               ;
               run;

data work.beta;
                infile datalines;
                input sam david karen mike;
                datalines;
             5 7 8 4
			 6 4 8 9
			 7 3 2 6
               ;
run;

data want_b;
if _n_=1 then do;
 dcl hash H (ordered: "A") ;
   h.definekey  ("_iorc_") ;
   h.definedata ("fix") ;
   h.definedone () ;
   dcl hiter hh('h');
do _iorc_=1 by 1 until(lr);
set alpha end=lr;
rc=h.add();
end;
end;
set beta;
array t(*)sam--mike;
do i=1 to dim(t);
rc=h.find(key:i);
t(i)=t(i)*fix;
end;
drop rc i nameo fix;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 01 Feb 2019 18:35:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiply-rows-in-one-dataset-by-a-column-in-another-dataset/m-p/532148#M145780</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-02-01T18:35:06Z</dc:date>
    </item>
    <item>
      <title>Re: Multiply rows in one dataset by a column in another dataset without any common variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiply-rows-in-one-dataset-by-a-column-in-another-dataset/m-p/532151#M145781</link>
      <description>&lt;P&gt;Novin&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The code works and avoids the need for creating so many macros.&lt;/P&gt;&lt;P&gt;Beautiful example and greatly appreciated.&lt;/P&gt;&lt;P&gt;Thank you,&lt;/P&gt;&lt;P&gt;Jane&lt;/P&gt;</description>
      <pubDate>Fri, 01 Feb 2019 18:36:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiply-rows-in-one-dataset-by-a-column-in-another-dataset/m-p/532151#M145781</guid>
      <dc:creator>jawhitmire</dc:creator>
      <dc:date>2019-02-01T18:36:38Z</dc:date>
    </item>
    <item>
      <title>Re: Multiply rows in one dataset by a column in another dataset without any common variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiply-rows-in-one-dataset-by-a-column-in-another-dataset/m-p/532152#M145782</link>
      <description>&lt;P&gt;Tom&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for the solution - it works perfectly&lt;/P&gt;&lt;P&gt;It is also my choice because the datasets are actually very large.&amp;nbsp; Thus, potentially using "proc transpose", as suggested by others, should perhaps be avoided as, at least in my mind and I could be totally wrong, this procedure weighs heavy on a LONG program that needs to run as quickly as possible. &amp;nbsp;&lt;/P&gt;&lt;P&gt;Creating a temporary array and adopting &amp;amp;sqlobs to monitor dimensions is ideal.&lt;/P&gt;&lt;P&gt;Thanks again,&lt;/P&gt;&lt;P&gt;Jane&lt;/P&gt;</description>
      <pubDate>Fri, 01 Feb 2019 18:42:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiply-rows-in-one-dataset-by-a-column-in-another-dataset/m-p/532152#M145782</guid>
      <dc:creator>jawhitmire</dc:creator>
      <dc:date>2019-02-01T18:42:42Z</dc:date>
    </item>
    <item>
      <title>Re: Multiply rows in one dataset by a column in another dataset without any common variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiply-rows-in-one-dataset-by-a-column-in-another-dataset/m-p/532156#M145786</link>
      <description>&lt;P&gt;Temporary array is a good idea but I think populating N macro vars is not needed when the PDV can hold&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
do _n_=1 by 1 until(lr);
set alpha end=lr;
array t(9999)_temporary_;
t(_n_)=fix;
end;
lr=0;
do until(lr);
set beta;
array j(*) sam--mike;
do _n_=1 to dim(j);
j(_n_)=j(_n_)*t(_n_);
end;
output;
end;
keep sam--mike;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 01 Feb 2019 18:55:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiply-rows-in-one-dataset-by-a-column-in-another-dataset/m-p/532156#M145786</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-02-01T18:55:39Z</dc:date>
    </item>
    <item>
      <title>Re: Multiply rows in one dataset by a column in another dataset without any common variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiply-rows-in-one-dataset-by-a-column-in-another-dataset/m-p/532260#M145841</link>
      <description>&lt;P&gt;I know there are already several good solutions here, but there's a technique that should not be overlooked.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename recode 'path to some file';

data _null_;
infile datalines;
input name $ fix;
file recode;
put name '=' name '*' fix ';' ;
datalines;
sam 0
david 3
karen 4 
mike 8
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;That creates a file holding all the recoding equations.&amp;nbsp; Then:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
infile datalines;
input sam david karen mike;
%include recode;
datalines;
5 7 8 4
6 4 8 9
7 3 2 6
;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 02 Feb 2019 04:20:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiply-rows-in-one-dataset-by-a-column-in-another-dataset/m-p/532260#M145841</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-02-02T04:20:15Z</dc:date>
    </item>
  </channel>
</rss>

