SAS Data Integration Studio, DataFlux Data Management Studio, SAS/ACCESS, SAS Data Loader for Hadoop and others

selecting unique cases

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

selecting unique cases

I need help with my SAS code. I have a dataset (test) that have variables yearcase and case. Yearcase is a variable that indicates a 2-motor vehicle crash .Case is a binary variable that indicates each of the 2 cars involved in the crash. I want to create a dataset (test2) that only includes case=1( car that caused the crash) and case=0 (car that did not cause the crash). I want each yearcase (crash pair) to have the car that caused the crash and one that did not cause the crash. I want to exclude yearcases (2-motor vehicle crashes) in which both cars did not cause the crash (case=0) or in which both cars caused the crash (case=1). My code here yields the following output below. As you can see, I still have yearcases (crashes) in which either both cases caused the crash (case=1) or both cases did not cause the crash (case=0). I attempted to use flag variables nocase and bothcase but its not working.

data test2; 
set test;
retain nocase bothcase;
by yearcase;
if first.yearcase then case=nocase;
if last.yearcase then case=bothcase;
if bothcase=nocase then output;
run;

proc freq data=test2;
tables yearcase*case/norow nocol nopercent;
run;

Output looks like this:

                        case 
 yearcase        case= 0    case= 1       Total
 201410001        0           2          2
 201410007        0           2          2
 201410015        2           0          2
 201410024        1           1          2
 201410031        1           1          2
 201410036        0           2          2

How can I make sure I only have yearcases (2-motor vehicle crashes) in which there is only 1 car that caused the crash (case=1) and another that did not (case=0)? I may need to use flag variables..but i'm unsure how to. Any help is greatly appreciated.

Thank you


Accepted Solutions
Solution
‎10-11-2017 04:46 PM
Super User
Posts: 6,629

Re: selecting unique cases

Here's an approach.  Assuming you always start with two observations for each YEARCASE:

 

proc sort data=have;

by yearcase case;

run;

 

data want;

set have;

by yearcase case;

if (case=0 and first.yearcase and last.case)

or (case=1 and last.yearcase and first.case);

run;

 

View solution in original post


All Replies
Solution
‎10-11-2017 04:46 PM
Super User
Posts: 6,629

Re: selecting unique cases

Here's an approach.  Assuming you always start with two observations for each YEARCASE:

 

proc sort data=have;

by yearcase case;

run;

 

data want;

set have;

by yearcase case;

if (case=0 and first.yearcase and last.case)

or (case=1 and last.yearcase and first.case);

run;

 

New Contributor
Posts: 2

Re: selecting unique cases

Posted in reply to Astounding

Brilliant! Yes, I always start with two observations for each YEARCASE. I tried this and it worked.

 

Thanks for your solution. 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 237 views
  • 0 likes
  • 2 in conversation