SAS 哈希 Table(右 Join/Union)
SAS Hash Table (Right Join/Union)
我正在寻找一种介于联接和联合之间的查找。我的主数据集中有大量记录,所以我想做一些不会成为多对多矩阵的 "brute force" 方法的事情。
这是我的主要数据集,名为 'All',其中已经包含列出的每个产品的价格。
product date price
apple 1/1/2011 1.05
apple 1/3/2011 1.02
apple 1/4/2011 1.07
pepper 1/2/2011 0.73
pepper 1/3/2011 0.75
pepper 1/6/2011 0.79
我的其他数据集('Prices' - 此处未显示,但包含相同的两个键,产品和日期)包含所有产品在每个可能日期的价格。我想创建的散列 table 查找基本上会查找 'All' table 中的每个日期,并输出 ALL 产品的价格那个日期,导致 table 像这样:
product date price
apple 1/1/2011 1.05
pepper 1/1/2011 0.71 *
apple 1/2/2011 1.04 *
pepper 1/2/2011 0.73
apple 1/3/2011 1.02
pepper 1/3/2011 0.75
apple 1/4/2011 1.07
pepper 1/4/2011 0.76 *
apple 1/6/2011 1.10 *
pepper 1/6/2011 0.79
也就是说,只要一个产品指定了日期和价格 'All' table,所有其他产品也应该从查找中提取 table。
星号表示价格是从价格 table 中查找的,包含产品价格的新行基本上插入到新的 table 中。
如果哈希 table 不是解决此问题的好方法,请告诉我其他方法。
好吧,这远非优雅,但很好奇下面是否给了您想要的结果?由于您在 ALL 中的每个键都有多个记录(我假设您想要维护),我基本上将 ALL 与 PRICES 中的记录联合起来,这些记录在 All 中有一个日期,但我添加了一个 Except 以排除已经在 ALL 中的记录.不知道这是否有意义,或者正在做你想做的事。当然不符合 'elegant' 的条件。
data all;
input product . date mmddyy10. price;
Y=1;
format date mmddyy10.;
cards;
apple 01/01/2011 1.05
apple 01/01/2011 1.05
apple 01/03/2011 1.02
pepper 01/02/2011 0.73
pepper 01/03/2011 0.75
pepper 01/06/2011 0.79
;
run;
data prices;
input product . date mmddyy10. price;
format date mmddyy10.;
cards;
apple 01/01/2011 1.05
apple 01/02/2011 1.04
apple 01/03/2011 1.02
apple 01/04/2011 1.07
apple 01/05/2011 1.01
pepper 01/01/2011 0.70
pepper 01/02/2011 0.73
pepper 01/03/2011 0.75
pepper 01/04/2011 0.76
pepper 01/05/2011 0.77
pepper 01/06/2011 0.79
;
run;
proc sql;
create table want as
select * from all
union corr all
( (select product,date,price from
prices
where date IN (select distinct date from all)
)
except corr
select product,date,price from all
)
;
quit;
我正在寻找一种介于联接和联合之间的查找。我的主数据集中有大量记录,所以我想做一些不会成为多对多矩阵的 "brute force" 方法的事情。
这是我的主要数据集,名为 'All',其中已经包含列出的每个产品的价格。
product date price
apple 1/1/2011 1.05
apple 1/3/2011 1.02
apple 1/4/2011 1.07
pepper 1/2/2011 0.73
pepper 1/3/2011 0.75
pepper 1/6/2011 0.79
我的其他数据集('Prices' - 此处未显示,但包含相同的两个键,产品和日期)包含所有产品在每个可能日期的价格。我想创建的散列 table 查找基本上会查找 'All' table 中的每个日期,并输出 ALL 产品的价格那个日期,导致 table 像这样:
product date price
apple 1/1/2011 1.05
pepper 1/1/2011 0.71 *
apple 1/2/2011 1.04 *
pepper 1/2/2011 0.73
apple 1/3/2011 1.02
pepper 1/3/2011 0.75
apple 1/4/2011 1.07
pepper 1/4/2011 0.76 *
apple 1/6/2011 1.10 *
pepper 1/6/2011 0.79
也就是说,只要一个产品指定了日期和价格 'All' table,所有其他产品也应该从查找中提取 table。 星号表示价格是从价格 table 中查找的,包含产品价格的新行基本上插入到新的 table 中。
如果哈希 table 不是解决此问题的好方法,请告诉我其他方法。
好吧,这远非优雅,但很好奇下面是否给了您想要的结果?由于您在 ALL 中的每个键都有多个记录(我假设您想要维护),我基本上将 ALL 与 PRICES 中的记录联合起来,这些记录在 All 中有一个日期,但我添加了一个 Except 以排除已经在 ALL 中的记录.不知道这是否有意义,或者正在做你想做的事。当然不符合 'elegant' 的条件。
data all;
input product . date mmddyy10. price;
Y=1;
format date mmddyy10.;
cards;
apple 01/01/2011 1.05
apple 01/01/2011 1.05
apple 01/03/2011 1.02
pepper 01/02/2011 0.73
pepper 01/03/2011 0.75
pepper 01/06/2011 0.79
;
run;
data prices;
input product . date mmddyy10. price;
format date mmddyy10.;
cards;
apple 01/01/2011 1.05
apple 01/02/2011 1.04
apple 01/03/2011 1.02
apple 01/04/2011 1.07
apple 01/05/2011 1.01
pepper 01/01/2011 0.70
pepper 01/02/2011 0.73
pepper 01/03/2011 0.75
pepper 01/04/2011 0.76
pepper 01/05/2011 0.77
pepper 01/06/2011 0.79
;
run;
proc sql;
create table want as
select * from all
union corr all
( (select product,date,price from
prices
where date IN (select distinct date from all)
)
except corr
select product,date,price from all
)
;
quit;