BookmarkSubscribeRSS Feed
CharlesR
Calcite | Level 5
Hello,
I'm not sure if this is the correct forum to pose this question, but . . .
I have 2 datasets which have the same variables/observations. For my project., i need to subtract one from the other, but i have a small problem. If i go to do it with proc iml, creating arrays with the values, iml does not work if a value=0, and i happen to have quite a few values of zero. Anyone have ideas of what i can do? Thanks!
4 REPLIES 4
Cynthia_sas
SAS Super FREQ
Hi:
First I had to make some data, so I used SASHELP.CLASS and made 2 different versions of it with some changed values (so there would be differences) and with some 0 values.

Then I used PROC SQL to join the two tables by NAME and do the subtraction. I kept EVERYTHING...so you could see what might be useful to you. If you did not want negative numbers, then you might use the ABS function to remove the - sign from the subtraction (as shown for the ABSAGEDIFF variable).

cynthia
[pre]
** make a version of SASHELP.CLASS with different values for;
** age, height and weight;
data class;
set sashelp.class;
age = age + 5;
height = height * 1.05;
weight = weight * 1.05;
** make a few 0 obs;
if name = 'John' then do;
age = 0;
height=0;
weight = 0;
end;
run;

** make some obs with 0 in a second copy of sashelp.class;
data class2;
set sashelp.class;
if name in ('Janet', 'Alfred') then do;
age = 0;
height=0;
weight = 0;
end;
run;

proc sql;
create table newdiff as
select a.name, a.sex, a.age, a.height, a.weight,
b.age as b_age, b.height as b_height, b.weight as b_weight,
a.age - b.age as agediff,
a.height - b.height as htdiff,
a.weight - b.weight as wtdiff,
abs(a.age - b.age) as absagediff
from work.class as a,
work.class2 as b
where a.name = b.name
order by name;
quit;

proc print data=work.newdiff;
run;
[/pre]
Reeza
Super User
You may also want to consider PROC COMPARE depending on what you're looking for, particularily if its validation.

Even If its just two data sets with same variables/observations in the same order then Proc Compare can help automate some of that, see

http://www.lexjansen.com/pharmasug/2003/tutorials/tu056.pdf
art297
Opal | Level 21
Charles,

If AND ONLY IF your data is exactly like you described, and there REALLY is a one for one match between the two files (i.e., if record N in the first file is always the same individual as record N in the second file) then a simple data step would also work.

For example, using Cynthia's test data (with one exception):

** make a version of SASHELP.CLASS with different values for;
** age, height and weight;
data class;
set sashelp.class;
age = age + 5;
height = height * 1.05;
weight = weight * 1.05;
** make a few 0 AND MISSINGobs;
if name = 'John' then do;
age = 0;
call missing(height);
weight = 0;
end;
run;

** make some obs with 0 in a second copy of sashelp.class;
data class2;
set sashelp.class;
if name in ('Janet', 'Alfred') then do;
age = 0;
height=0;
weight = 0;
end;
run;

data want;
set class;
set class2(keep=age height weight
rename=(age=b_age
height=b_height
weight=b_weight));
age_diff=sum(age-b_age);
height_diff=sum(height-b_height);
weight_diff=(weight-b_weight);
run;

HTH,
Art
CharlesR
Calcite | Level 5
hey thanks both of you! I think i'm going with the data step! Appreciate the help as always!

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
  • 4 replies
  • 7270 views
  • 0 likes
  • 4 in conversation