BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
stunt
Calcite | Level 5

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

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

2 REPLIES 2
Astounding
PROC Star

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;

 

stunt
Calcite | Level 5

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

 

Thanks for your solution. 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

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