基于值添加新列(Adding a new column based on values)
我有以下示例数据:
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 =“red”,“yellow”和list2 =“purple”和“green”) 根据是否在列表1和列表2中分类记录并添加新列。所以伪代码是这样的:
'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关于如何最有效地做到这一点的任何想法?
I have the following sample data:
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 ;What I would like to do now is the following:
Create two lists with colours (fe, list1 = "red" and "yellow" and list2 = "purple" and "green") Classify the records according to whether or not they are in list1 and list2 and add a new column.So the pseudo code is like this:
'Set new category called class If item is in list1 then class = 1 Else if item is in list2 then class = 2 Else class = 3Any thoughts on how I can do this most effciently?
最满意答案
你的伪代码几乎就是它。
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.;Your pseudocode is almost exactly it.
If item is in ('red' 'yellow') then class = 1; Else if item is in ('purple' 'green') then class = 2; Else class = 3;This is really a lookup, so their are many other methods. One I usually recommend as well is Proc format, though in a simplistic case like this I'm not sure of any gains.
Proc format; Value $ colour_cat 'red', 'yellow' = 1 'purple', 'green' = 2 Other = 3; Run;And then in a data/SQL either of the following can be used.
*actual conversion; Category = put(colour, $colour_cat.); * change display only; Format colour $colour_cat.;更多推荐
发布评论