用if if else进行数据转换

时间:2015-06-25 20:28:58

标签: 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和subj的术语是什么。所以意味着:

  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; 

2 个答案:

答案 0 :(得分:0)

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

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,但如果您想要更具体的解决方案,则必须提供更多信息。

答案 1 :(得分:0)

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;