我目前正在使用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中实现这一目标吗?真的很感激:))
答案 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;