BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
jawhitmire
Quartz | Level 8

Version 9.4 with no access to library SAS/STATS or IML

 

The following source code is a condensed version of 2 datasets.  Actual data is longer with several decimal places of accuracy.

data work.have1;
	input a b c;
datalines;
5 6 7
8 9 4
3 2 3
;
run;
data work.have2;
	input a b c;
datalines;
4 5 6
2 3 4
9 7 2
;
run;

Desired output is product of corresponding elements:

a b c

20 30 42

16 27 16

27 14 6

 

The following is the first attempt at coding. It compiles without errors, but contains 6 rows and 3 columns whereas the datasets are appended to one another:

proc sql;

create table work.product1 as

select * from work.have1

outer union corr

select * from work.have2; 

quit;

The following is the second attempt at coding.  It compiles without errors, but does not produce the desired results:

proc fcmp;
	array h1[3,3] /nosymbols;
	array h2[3,3] /nosymbols;
	array product[3,3] ;
	re=read_array('work.have1',h1);
	re=read_array('work.have2',h2);
	call mult(h1,h2,product);
	rc=write_array('work.product',product);
run;

Thank you in advance for your support, insight, and willingness to assist.

 

Sincerely,

 

Jane

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
data work.have1;
	input a1 b1 c1;
datalines;
5 6 7
8 9 4
3 2 3
;
run;
data work.have2;
	input a2 b2 c2;
datalines;
4 5 6
2 3 4
9 7 2
;
run;

data want;
    merge have1 have2;
    result_a=a1*a2;
    result_b=b1*b2;
    result_c=c1*c2;
    keep result:;
run;
--
Paige Miller

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26
data work.have1;
	input a1 b1 c1;
datalines;
5 6 7
8 9 4
3 2 3
;
run;
data work.have2;
	input a2 b2 c2;
datalines;
4 5 6
2 3 4
9 7 2
;
run;

data want;
    merge have1 have2;
    result_a=a1*a2;
    result_b=b1*b2;
    result_c=c1*c2;
    keep result:;
run;
--
Paige Miller
ccaulkins9
Pyrite | Level 9
I especially like the product-matrix, or is it matrix product?, it takes me back to early Computer Science programming exercises in Pascal, C, etc...
e-SAS regards,

jawhitmire
Quartz | Level 8

Thank you for the feedback.

 

Looks like it will be necessary to create additional variable (column) names.

 

No worries, I can do that.

 

Cheers!

 

Jane

Tom
Super User Tom
Super User

Assuming that both HAVE1 and HAVE2 have exactly the same variables then here is method that will work without having to know the names of the variables.

Use a temporary array to hold the values for the first dataset.

Just make sure to make the temporary array as large or larger than the number of variables.

 

data want ;
  set have1;
  array x _numeric_;
  array y [1000] _temporary_;
  do _n_=1 to dim(x);
    y[_n_]=x[_n_];
  end;
  set have2;
  do _n_=1 to dim(x);
    x[_n_]=y[_n_]*x[_n_];
  end;
run;

 

jawhitmire
Quartz | Level 8

Tom,

Wow - something complicated made simple. 

Thank you!

Jane

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

5 Steps to Your First Analytics Project Using SAS

For SAS newbies, this video is a great way to get started. James Harroun walks through the process using SAS Studio for SAS OnDemand for Academics, but the same steps apply to any analytics project.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 5 replies
  • 718 views
  • 5 likes
  • 4 in conversation