The SAS Output Delivery System and reporting techniques

multiply two tables together

Reply
New Contributor
Posts: 3

multiply two tables together

I've got 2 tables that I want multiplied together:

Table1 Table 2
r1c1 r1c2 r1c3 r1c1 r1c2 r1c3
r2c1 r2c2 r2c3 r2c1 r2c2 r2c3

result should be:
r1c1*r1c1 r1c2*r2c2 ....
r2c1*r2c1 .....

can someone help? Thanks
Super Contributor
Super Contributor
Posts: 3,174

Re: multiply two tables together

Look at using an ARRAY within a DATA step to reference your SAS variables more conveniently than each individual one. Then you can use a DO / END paragraph along with the ARRAY and appropriate/desired subscript to compute some new / existing (?) SAS variable, as needed.

Scott Barry
SBBWorks, Inc.

Suggested Google advanced search argument, this topic / post:

using arrays data step site:sas.com
SAS Super FREQ
Posts: 8,868

Re: multiply two tables together

Hi:
Are your variables actually named r1c1, r1c2, r1c3, etc???? Or, are they more meaningful names like NAME, AGE1, HT1, WT1 in Table1 and perhaps NAME, AGE2, HT2 and WT2 in Table2??? Is there any identifier on the rows/observations, such as NAME?? Does each dataset have EXACTLY the same number of observations?? Do you want ROW1 in the first table matched to ROW1 in the second table -- ALWAYS??

Generally speaking, the "row" number or observation number is not fixed or saved in a SAS dataset and can be impacted by sort order. So for example, SASHELP.CLASS has 19 rows/observations. If I sort the file by ascending name, then Alfred's observation will become Row 1 in the file. But if I sort by SEX and then NAME, then Alice's observation will become Row 1 in the file. So if you run this code, you will see that observation #1 will change depending on the sort order used for the file:
[pre]
proc sort data=sashelp.class out=class;
by name;
run;

proc print data=class;
title 'Sort Order by Name';
run;

proc sort data=sashelp.class out=class;
by sex name;
run;

proc print data=class;
title 'Sort Order by Name within Sex';
run;
[/pre]

It seems to me that there is some important information missing in your description of the problem. While Scott's suggestion might be the way to go, depending on whether you have identifiers in each table that you have to match or whether each table has exactly the same number of rows, you might choose a different approach.

cynthia
Super User
Posts: 10,041

Re: multiply two tables together

[pre]


data one;
count+1;
set sashelp.class(keep=height);
run;
data two;
count+1;
set sashelp.class(keep=weight);
run;

data want;
merge one two;
by count;
want=height*weight;
run;
[/pre]

But you need to rename variables firstly if these two tables have the same variable name.


Ksharp
Ask a Question
Discussion stats
  • 3 replies
  • 1471 views
  • 0 likes
  • 4 in conversation