2016-09-23 79 views
0

我有下面的示例数据:将基于值的新列

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,列表1 =“红色”和“黄色”和列表2 =“紫色”和“绿色”)
  • 根据它们是否在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 

我如何能做到这一点最有效牙缝有什么想法?

回答

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.;