计算依赖于日期变量的值



如果"SUBJECT"列中的两个值匹配,并且"PROCEDURE"列中有两个值相匹配,我正试图计算上次测量的值(根据日期列(除以记录的最低值(根据测量列(。计算结果将在一个新列中生成。我在这方面遇到了麻烦,我希望能解决这个问题。

data Have;
input Subject Type :$12. Date &:anydtdte. Procedure :$12. Measurement;
format date yymmdd10.;
datalines;
500   Initial    15 AUG 2017     Invasive     20 
500   Initial    15 AUG 2017     Surface      35 
500   Followup   15 AUG 2018     Invasive     54 
428   Followup    15 AUG 2018    Outer        29 
765   Seventh     3 AUG 2018     Other        13 
500   Followup    3 JUL 2018     Surface      98 
428   Initial     3 JUL 2017     Outer        10 
765   Initial     20 JUL 2019    Other        19 
610   Third       20 AUG 2019    Invasive     66 
610   Initial     17 Mar 2018    Invasive     17 
;
*Intended output table
Subject Type      Date           Procedure    Measurement     Output
500   Initial    15 AUG 2017     Invasive        20            20/20
500   Initial    15 AUG 2017     Surface         35            35/35
500   Followup   15 AUG 2018     Invasive        54            54/20
428   Followup    15 AUG 2018    Outer           29            29/10
765   Seventh     3 AUG 2018     Other           13            13/19
500   Followup    3 JUL 2018     surface         98            98/35
428   Initial     3 JUL 2017     Outer           10            10/10
765   Initial     20 JUL 2019    Other           19            19/19
610   Third       20 AUG 2019    Invasive        66            66/17
610   Initial     17 Mar 2018    Invasive        17            17/17 ;
*Attempt;
PROC SQL;
create table want as
select a.*,
(select measurement as measurement_last_date
from have
where subject = a.subject and type = a.type 
having date = max(date)) / min(a.measurement) as ratio
from have as a
group by subject, type
order by subject, type, date;
QUIT;

我认为您需要使用语句retain数据步骤

该语句将保留最后一行,您可以将最后一行与实际处理的行进行比较。

关于如何保留use语句的一些教程的链接。在此处输入链接描述

SAS文档在此处输入链接描述

最新更新