我试图在两个条件下选择第一次出现的记录但是徒劳无功。这是我的代码:
PROC SQL;
CREATE TABLE table1 AS
SELECT user_id, type, date, money
FROM table2
WHERE date IN (SELECT MIN(date)
FROM twice_transaction
GROUP BY user_id,type);
例如,原始表格如下(table2)
user type date money
user1 type1 1/10/2012 money1
user1 type1 2/20/2012 money2
user1 type2 1/15/2012 money3
user1 type2 2/30/2012 money4
user2 type1 3/28/2012 money5
user2 type2 2/14/2012 money6
user2 type2 4/13/2012 money7
但我只想:( table1)
user1 type1 1/10/2012 money1
user1 type2 1/15/2012 money3
user2 type1 3/28/2012 money5
user2 type2 2/14/2012 money6
我应该如何修改/编码我的最终结果?谢谢!
答案 0 :(得分:1)
使用SQL有两种方法可以做到这一点。 @NoDisplayName的评论向您展示了一种更传统的SAS方式来实现这一目标。
CREATE TABLE table1 AS
SELECT a.user_id, a.type, a.date, a.money
FROM table2 as a
INNER JOIN
SELECT (user_id, type, min(date) as date from table2 group by user_id, type) as b
on a.user_id = b.user_id
and a.type = b.type
and a.date = b.date;
我在这里做的是创建一个内部选择,通过user_id和类型获取最小日期。然后我使用内部联接来仅选择第一个表中与第二个表对齐的记录。
答案 1 :(得分:1)
使用HAVING子句也是一种选择。
data have;
informat usert type $8. date mmddyy10. money $8.;
format date date9.;
input usert type date money;
cards;
user1 type1 1/10/2012 money1
user1 type1 2/20/2012 money2
user1 type2 1/15/2012 money3
user1 type2 2/28/2012 money4
user2 type1 3/28/2012 money5
user2 type2 2/14/2012 money6
user2 type2 4/13/2012 money7
;
run;
proc sql;
create table want as
select usert, type, date, money
from have
group by usert, type
having date=min(date);
quit;