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
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;
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;
Brilliant! Yes, I always start with two observations for each YEARCASE. I tried this and it worked.
Thanks for your solution.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.