BookmarkSubscribeRSS Feed
deleted_user
Not applicable
Hi guys.

I have two data sets that looks like this:

data a;
input month monthly_sum sum1 sum2 sum3 sum4 sum5;
cards;
1 10 1 1 1 1 1
2 20 2 2 2 2 .
3 30 3 3 3 . .
4 40 4 4 . . .
5 50 5 . . . .
6 60 . . . . .
;

data b;
input month monthly_sum total1 total2 total3 total4 total5;
cards;
1 100 10 10 10 10 10
2 200 20 20 20 20 .
3 300 30 30 30 . .
4 400 40 40 . . .
5 500 50 . . . .
6 600 . . . . .
;

What I want to do is create a new dataset that divides all the cells for sum1-sum5 for every row from dataset a with the "same location" cells for total1-total5 from dataset b.

The result should look like this:
data result;
input month monthly_sum calc1 calc2 calc3 calc4 calc5;
cards;
1 10 0.1 0.1 0.1 0.1 0.1
2 20 0.1 0.1 0.1 0.1 .
3 30 0.1 0.1 0.1 . .
4 40 0.1 0.1 . . .
5 50 0.1 . . . .
6 60 . . . . .
;

For example:
first row (month=1) : dataset a.sum1/dataset b.total1 = 1/10 = 0.1
first row (month=1) : dataset a.sum2/dataset b.total2 = 1/10 = 0.1...etc
second row (month=2) : dataset a.sum1/dataset b.total1 = 2/20 = 0.1
second row (month=2) : dataset a.sum2/dataset b.month2 = 2/20 = 0.1 ...etc
.
.
etc

I guees arrays would be perfect for this, but how do you use arrays when there's two datasets involved?

Do you have any ideas ? : )
2 REPLIES 2
deleted_user
Not applicable
You could use the MERGE statement with a "BY MONTH;" and then set up arrays for the SUM's and TOTAL's. You may have to sort each data set BY MONTH if they are not already in order.
deleted_user
Not applicable
Yeah thanks. I tried something like that, it seems to work : )

proc sql;
create table testing as select * from a as one inner join b as two
on one.month=two.month;
quit;

data testar;
set testing;
array y(*) total1-total5;
array z(*) sum1-sum5;
array a{5} ;
do i=1 to 5;
if z(i)>0 then a(i)=z(i)/y(i);
else a(i)=0;
end;
run;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 810 views
  • 0 likes
  • 1 in conversation