[英]How to fill column using values in 2 other columns in PROC SQL / SAS code in SAS Enterprise Guide?
I have table in SAS Enterprise Guide like below:我在 SAS 企业指南中有如下表:
Data types:数据类型:
VAL1 - character VAL1 - 字符
ID - numeric ID - 数字
VAL2 - character (with only 3 possible values: P, C, S) VAL2 - 字符(只有 3 个可能的值:P、C、S)
ID ID | EVENT事件 | GRADE年级 |
---|---|---|
123 123 | NY纽约 | P磷 |
123 123 | NY纽约 | P磷 |
123 123 | NY纽约 | S小号 |
55 55 | MAD疯狂的 | C C |
55 55 | MAD疯狂的 | S小号 |
55 55 | LON伦敦 | S小号 |
ID - column with ID ID - 带有 ID 的列
EVENT - event name EVENT - 事件名称
GRADE - evaluation of the event (mark) GRADE - 事件评估(标记)
And I need to create table where will be ID, EVENT and GRADE of event, but:我需要创建一个表,其中将是事件的 ID、EVENT 和 GRADE,但是:
So as a result I need something like below:因此,我需要以下内容:
ID |EVENT|GRADE
----|-----|----
123 | NY | P --> because P is more important than S
55 | MAD | C --> because C is more important than S
55 | LON | S --> because has only S
How can I do that in normal SAS code or in PROC SQL in Enterprise Guide?如何在普通 SAS 代码或企业指南中的 PROC SQL 中执行此操作? :) :)
Here is a SQL-based solution that will work assuming C
and P
only occur once per id & event.这是一个基于 SQL 的解决方案,假设C
和P
仅在每个 id 和事件中出现一次。 We'll assign values to C
, P
, and S
:我们将为C
、 P
和S
赋值:
C, P = 1
S = 2
If we take the minimum of those values per (id, event)
, we will be left with a unique list of IDs and events in a hierarchical order.如果我们在每个(id, event)
中取最小值,我们将得到一个按层次顺序排列的唯一 ID 和事件列表。
proc sql;
create table want(drop=value) as
select distinct
id
, event
, grade
, CASE when grade IN('C', 'P') then 1 else 2 END as value
from have
group by id, event
having value = min(value)
;
quit;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.