humanmaycry
07-01-2008, 11:49 PM
i have two sas-data-set and they have the same variables.
of course including character and numeric.
i want do a data set minus, which means use one data set to minus the other one.
of course it is numerical variables minus, keep the character variables the same.
for example:
data set 1: data set 2:
game game
xbox 35 xbox 22
xbox360 42 xbox360 33
ps3 40 ps3 17
wii 38 wii 21
and i want to get a data set as
game
xbox 23
xbox360 9
ps3 23
wii 17
vinux
07-02-2008, 12:00 AM
Easy way is by using proc sql
select
a.game,
(a.xbox - b.xbox) as xbox ,
(a.xbox360 - b.xbox360) as xbox360 ,
(a.ps3 - b.ps3) as ps3 ,(a.wii - b.wii) as wii
from dataset1 a , dataset2 b
where a.game=b.game
I hope the you are connecting by game. you can use merge also for the same.
and one thing ( 35-22 =13 it s not 23)
Regards
Vinux
humanmaycry
07-02-2008, 01:06 PM
Thanks Vinux, i am very appreciated your help.
the fact is, i use game as a example. actually i have nearly 1000 observations.
i can not use your way to do it one by one.
let me show you what i really need to do.
country year birth
U.K. 2000 28.2
U.K 2001 23.1
and i want to get a data set like
country year birth_diff
U.K. 2000 5.1
first i want to subset two data set of different years, then minus each other.
i did it by using merge statement, but i think that is a little troublesome.
first subset two data sets, then change variables names and merge as a new one, then make calculation as a new one. i used four new data set to do this. is there a better way?
thanks,
regards
Mean Joe
09-17-2008, 02:47 PM
I realize that the original post was months ago, but maybe this can still be helpful to someone out there.
You can sort your original dataset, then make a new datastep and use the set.../by... "trick".
proc sort data=TwoLineData; by country DESCENDING year; run;
data DataDiff;
retain birth_prev; *var to store the BIRTH value from previous year;
set TwoLineData;
by country; *using the by statement, you now have access to functions called first. and last. ;
if first.country then birth_prev=birth;
if last.country then birth_diff = birth_prev - birth;
output DataDiff; *explicitly call the output statement, so that data is output exactly once per country;
run;
I'm pretty sure this will work, but it may not. Anyway, the main point is to use the by statement, so that you can use the last. and first. functions