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-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!

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.

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
  • 2 replies
  • 625 views
  • 0 likes
  • 1 in conversation