根据值添加新列

时间:2016-09-23 08:49:53

标签: sas

我有以下示例数据:

data weight_club;
  input IdNumber 1-4 Name $ 6-24 Team $ StartWeight EndWeight;
  Loss = StartWeight - EndWeight;
  datalines;
1023 David Shaw         red    189 165
1049 Amelia Serrano     yellow 145 124
1219 Alan Nance         purple   210 192
1246 Ravi Sinha         yellow 194 177
1078 Ashley McKnight    green    127 118
;

我现在想做的是:

  • 使用颜色创建两个列表(fe,list1 ="红色"和"黄色"和list2 ="紫色"和"绿色" )
  • 根据记录是否在list1和list2中对记录进行分类并添加新列。

所以伪代码是这样的:

'Set new category called class

If item is in list1 then class = 1
Else if item is in list2 then class = 2
Else class = 3

关于我如何能够最有效地做到这一点的任何想法?

1 个答案:

答案 0 :(得分:0)

您的伪代码几乎就是它。

If item is in ('red' 'yellow') then class = 1;
Else if item is in ('purple' 'green') then class = 2;
Else class = 3;

这实际上是一个查找,因此它们有许多其他方法。我通常推荐的还有Proc格式,虽然在这种简单的情况下,我不确定是否有任何收益。

Proc format;
    Value $ colour_cat
    'red', 'yellow' = 1
     'purple', 'green' = 2
      Other = 3;
Run;

然后在数据/ SQL中可以使用以下任一项。

*actual conversion;
Category = put(colour, $colour_cat.);

 * change display only;
 Format colour $colour_cat.;