SAS基于现有组生成新列

时间:2013-07-09 20:03:08

标签: sas

我目前正在使用SAS编写报告,其中包含如下表格:

Name Country  Pct  Flag
A      USA     40   Y
A      CAN     30   N
A      CHN     30   N
B      BRA     70   N
B      JAP     30   Y

我想生成一个新列Name_Flag,该列等于该pct具有最高name的记录的标记。例如,A的name_flag应为Y,而B应为N.

有人能给我一点点如何在SAS中实现这一目标吗?真的很感激:))

3 个答案:

答案 0 :(得分:2)

这里有一个稍微简单的解决方案。

data have;
input Name $ Country $ Pct  Flag $;
datalines;
A      USA     40   Y
A      CAN     30   N
A      CHN     30   N
B      BRA     70   N
B      JAP     30   Y
;
run;

proc sort data=have;
by name descending pct;
run;

data want;
set have;
by name descending pct;
retain name_flag;
if first.name then name_flag=flag;
run;

答案 1 :(得分:0)

您可能需要调整此项,因为我没有运行SAS会话来测试。

proc sort data = flagData;
  by pct descending;
run;

data flagDataDone;
  retain nameWithHighestPct;
  set flagData;
  if _n_ = 1 then do;
    nameWithHighestPct = name;
  end;
  name_flag = 'N';
  if name = nameWithHighestPct then do;
    name_flag = 'Y';
  end;
  drop nameWithHighestPct;
run;

答案 2 :(得分:0)

编辑:基思的答案更简单,更清晰。只有当您的数据已按名称排序且数据集很大时,我才建议采用我的方法,因为它不需要另一种排序。否则,坚持基思的方法。

假设数据已按名称排序:

*First we find the correct flag per group;
data BEST_PER_GROUP (keep=Name Name_Flag);
    set DATASET;
    by Name;

    *Need to retain this until we looked at all candidates;
    retain highest_pct 0;
    retain Name_Flag '';

    *Find the flag of the highest Pct;
    if Pct > highest_pct then do;
        highest_pct = Pct;
        Name_Flag = Flag;
    end;

    *When having looked at all records for a given Name, output the result;
    if last.Name then do;
        output;
        *Reset for next value group of Name;
        highest_pct = 0;
        Name_Flag = '';
    end;
run;

*Merge it back with your data;
data DATASET;
    merge DATASET BEST_PER_GROUP;
    by Name;
run;