If statement 使用if-then-else进行数据转换

If statement 使用if-then-else进行数据转换,if-statement,sas,If Statement,Sas,我的表格如下: id term subj degree 18 2007 ww Yes 32 2015 AA Yes 32 2016 AA No 25 2011 NM No 25 2001 ts No 18 2009 ww Yes 18 2010 ww No 如果度为Yes,我需要另一个变量term2,我将向term2写入任何

我的表格如下:

  id   term   subj   degree
  18    2007   ww     Yes
  32    2015   AA     Yes
  32    2016   AA     No
  25    2011   NM     No
  25    2001   ts     No

  18    2009   ww     Yes
  18    2010   ww     No
如果度为Yes,我需要另一个变量term2,我将向term2写入任何相同的id和subc的术语。这意味着:

  id    term   subj   degree   term2
  18    2007   ww     Yes       2009
  32    2015   AA     Yes       2016
  32    2016   AA     No         0
  25    2011   NM     No         0
  25    2001   ts     No         0

  18    2009   ww     Yes       2010  
  18    2010   ww     No         0
如果其他方法不起作用,我会怎么做。有什么想法吗?多谢各位

这是我用过的

  data have;
    merge aa aa (rename=(id=id1 subj=subj1     
        term=term1);
    term2=0;
    if id=id1 and subj=subj1 and degree="Yes" then
     term2=term1

   run; 

缺少一些重要信息,例如,当一个id有一个degree=yes值时,是否总是有一个degree=no行具有相同的id? 如果一个id有一个degree=yes值,并且有多个degree=no行具有不同的术语,那么应该怎么做?为什么要用if-else语句来解决这个问题? 假设对于degree=yes的行,始终只有一个id匹配degree=no行,则可以使用以下方法:

Proc sql;
 Select a.*, case when a.degree = "Yes" then b.term else 0 end from table as a
 left outer join table as b on a.id = b.id and b.degree = "No" and a.degree="Yes";
quit;

这没有if语句,也没有datastep,但如果您想要更具体的解决方案,则必须提供更多信息。

您使用if-then-else做了什么?以什么方式不起作用?请显示您迄今为止尝试过的代码。错误:WHEN子句2的结果与前面的结果的数据类型不同。我根据您的问题更改了表。它起作用,但有一个原因不是所有结果都起作用。许多“是”值都有“0”项2。其中一些是对的,我根据为什么不起作用更新了这个问题。仅当相同的值紧跟在其他值之后时,它才起作用。否则不起作用。因此,您必须首先按id对数据进行排序。
data have;
input id   term   subj $  degree $;
cards;
  32    2015   AA     Yes
  32    2016   AA     No
  25    2011   NM     No
  25    2001   ts     No
  18    2007   ww     Yes
  18    2010   ww     No
;

data want;
   merge have have(firstobs=2 keep=id term rename=(id=_id term=_term));
   term2=0;
   if id=_id and degree='Yes' then term2=_term;
   drop _:;
run;