unbik
unbik

Reputation: 186

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:

Data types:

And I need to create table where will be ID, EVENT and GRADE of event, but:

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 ? :)

Upvotes: -1

Views: 356

Answers (1)

Stu Sztukowski
Stu Sztukowski

Reputation: 12909

Here is a SQL-based solution that will work assuming C and P only occur once per id & event. We'll assign values to C, P, and 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.

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;

Upvotes: 0

Related Questions