[英]Return most recent value using proc SQL in SAS
I'm looking to return the most recent result where the letter_source is "CCMA", based on the date the letter_type is "P29". 我想根据letter_type为“ P29”的日期返回letter_source为“ CCMA”的最新结果。
Basically I need to know the date of the last "CCMA" letter only for accounts which have had a "P29" letter. 基本上,我只需要知道有“ P29”字母的帐户的最后“ CCMA”字母的日期即可。 The CCMA letter can be any date as long as it's the most recent, but the P29 must be within a specific time period. CCMA字母可以是任何最近的日期,但是P29必须在特定时间段内。
So far my code is: 到目前为止,我的代码是:
proc sql;
select distinct
account_no as account_no
,letter_date as letter_date
,letter_type as letter_type
,source as letter_source
from uulster.perm_aura_letters
where letter_type="P29"
and letter_date >= '01jul2015'd and letter_date <= '09jul2015'd
order by letter_date, letter_type;
quit;
Please could you help? 请你能帮忙吗?
proc sql;
select * from (select distinct
account_no as account_no
,letter_date as letter_date
,letter_type as letter_type
,source as letter_source
from uulster.perm_aura_letters
where letter_type="P29"
and letter_source contains "CCMA"
and letter_date >= '01jul2015'd and letter_date <= '09jul2015'd)
having letter_date=max(letter_date)
order by letter_date, letter_type
;
quit;
I think you can do this using 2 perm letter tables. 我认为您可以使用2个烫发字母表格来完成此操作。 This isn't pretty but I'm not coming up with a more efficient way right now... 这不是很漂亮,但是我现在还没有想出一种更有效的方法...
select distinct
account_no as account_no
,letter_date as letter_date
,letter_type as letter_type
,source as letter_source
from uulster.perm_aura_letters p29
join uulster.perm_aura_letters ccma on p29.account_no = ccma.account_no
where p29.letter_type="P29"
and ccma.letter_type = "CCMA"
and p29.letter_date >= '01jul2015'd and p29.letter_date <= '09jul2015'd
and ccma.letterdate = (subselect max date for account and ccma)
order by p29.letter_date, p.29.letter_type
; ;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.