Search code examples
sqlsasproc-sqldatastep

SAS - Select from multiple records with same id but different status


Sorry for the misleading (if there is) question title, as I don't know how to express what I need in 1 question.

I have a dataset as below:

UserId    Order    Status
1         1        completed
1         2        completed
1         3        incompleted
2         1        incompleted
2         2        incompleted

I want to select those users who have no completed status, for example, with the above dataset the result I get would be

UserId
2

Is there anyway to get the above result with SAS datastep or PROC SQL? Help is appreciated and thanks in advance.


Solution

  • Here's a data step approach, the proc sort will not be necessary if your data is already sorted.

    data have;
    input UserId Order Status :$11.;
    datalines;
    1         1        completed
    1         2        completed
    1         3        incompleted
    2         1        incompleted
    2         2        incompleted
    ;
    run;
    
    proc sort data=have;
    by userid order;
    run;
    
    data want (keep=userid);
    set have;
    by userid;
    if first.userid then num_complete=0;
    num_complete+(status='completed');
    if last.userid and num_complete=0 then output;
    run;