Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Date 根据日期重新分配值_Date_Sas - Fatal编程技术网

Date 根据日期重新分配值

Date 根据日期重新分配值,date,sas,Date,Sas,我需要比较日期,并根据ID将值重新分配给两个新变量 如果同一id有两个日期,则: 如果'date'变量更早,则应将其值重新指定为“更早的状态”。 如果'date'变量更晚,则应将其值重新指定为“Current status” 如果id只有一个日期,则该值将被重新分配到“当前状态”。“早期状态”需要缺失 如果id有两个以上的日期,则中间日期的值将被忽略,仅使用较早和最新的值 有什么想法吗?非常感谢 这是我尝试过的代码: data origin; input id date mmddyy8. sta

我需要比较日期,并根据ID将值重新分配给两个新变量

如果同一id有两个日期,则: 如果'date'变量更早,则应将其值重新指定为“更早的状态”。 如果'date'变量更晚,则应将其值重新指定为“Current status”

如果id只有一个日期,则该值将被重新分配到“当前状态”。“早期状态”需要缺失

如果id有两个以上的日期,则中间日期的值将被忽略,仅使用较早和最新的值

有什么想法吗?非常感谢

这是我尝试过的代码:

data origin;
input id date mmddyy8. status;
datalines;
1 1/1/2010 0
1 1/1/2011 1
2 2/2/2002 1
3 3/3/2003 1
3 2/5/2010 0
4 1/1/2000 0
4 1/1/2003 0
4 1/1/2005 1

;
run;

proc print; format date yymmdd8.; run;

proc sort data=origin out=a1;
by id date;
run;

data need; set a1;
if first.date then EarlierStatus=status;
else if last.date then CurrentStatus=status;
by id;
run;

proc print; format date yymmdd8.; run;

此过程sql将获得您想要的:

proc sql;
create table need as
select distinct
t1.id,
t2.EarlierStatus,
t1.CurrentStatus
from (select distinct
      id,
      date,
      status as CurrentStatus
      from origin
      group by id
      having date=max(date)) as t1
left join (select distinct
           id,
           date,
           status as EarlierStatus
           from origin 
           group by id
           having date ~= max(date)) as t2 on t1.id=t2.id;
quit;
上面的代码有两个子查询。在第一个子查询中,您只保留id为“日期最大值”的行,并将状态重命名为CurrentStatus。在第二个子查询中,将保留所有不具有“按id显示的最大日期”的行,并将状态重命名为EarlyStatus。因此,如果您的原始表对于一个id只有一个日期,那么它也是最大值,您将在第二个子查询中删除这一行。然后在第一个子查询和第二个子查询之间执行左连接,将EarlyStatus从第二个子查询拉入第一个子查询。如果没有找到EarlyStatus,那么它就不见了


最好的,所以,有几件事。首先-注意对代码的一些更正-特别是
,如果要使用混合列表样式输入,这一点至关重要

第二;您需要保留EarlierStatus。否则,它将在每次数据步骤迭代中清除

第三,您需要使用
first.id
而不是
first.date
(类似于
last
)-
first
所做的是说“这是
id
的新值的第一次迭代”。日期是你用英语说的(“第一次约会…”)

最后,您需要更多的测试来设置变量

data origin;
input id date :mmddyy10. status;
format date mmddyy10.;
datalines;
1 1/1/2010 0
1 1/1/2011 1
2 2/2/2002 1
3 3/3/2003 1
3 2/5/2010 0
4 1/1/2000 0
4 1/1/2003 0
4 1/1/2005 1

;
run;

proc sort data=origin out=a1;
by id date;
run;

data need; 
    set a1;
    by id;
    retain EarlierStatus;
    if first.id then call missing(EarlierStatus);   *first time through for an ID, clear EarlierStatus;
    if first.id and not last.id then EarlierStatus=status;  *if it is first time for the id, but not ONLY time, then set EarlierStatus;
    else if last.id then CurrentStatus=status;  *if it is last time for the id, then set CurrentStatus;
    if last.id then output;  *and if it is last time for the id, then output;
run;

我在那里做的if/else可能会略有不同,具体取决于你想做什么,我试图让事情彼此之间的关系更直接一些。

正如我上次提到的那样-请将你的数据作为数据步骤包含(
数据有;输入id日期:mmddyy10.status;datalines;
等等)而不是作为一幅画。我无法编写程序将您的图片输入SAS。另外-请尝试解决此问题,并提供相应的代码。“给我写一个程序来做这个”不是这里的主题。谢谢你,乔!我已经附上了代码,我使用了first.date和By语句,但是结果都没有告诉你这么多,这个“:”很有用!这里的逻辑对我来说非常清楚!似乎我不需要在一开始就把“保留早期状态”指定为缺失,哈谢谢你!我认为左连接在这里很重要,就像我使用右连接一样,它不会得到想要的结果