BookmarkSubscribeRSS Feed
pcbc
Calcite | Level 5
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
3 REPLIES 3
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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
Cynthia_sas
SAS Super FREQ
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
Ksharp
Super User
[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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 3044 views
  • 0 likes
  • 4 in conversation