I have a list of diagnosis codes, and I want to flag if any of those codes exist anywhere in 15 different diagnosis variables. I created a series of IF-THEN statements, but I have many such lists of DX codes and an array is a more efficient approach. However, the array is only flagging a very small percentage of cases that should be flagged (I confirmed this by manually searching the data file). The cases that it does flag hit on various DX variables (DX1, DX3, DX12, etc), so it's not like the array is only searching DX15 or something like that.
%let PTB_all_list = ("O6010X","O6012X","Z3A22","Z3A23","Z3A24","Z3A25","Z3A26","Z3A27","O6013X",
"O6014X","Z3A28","Z3A29","Z3A30","Z3A31","Z3A32","Z3A33","Z3A34","Z3A35",
"Z3A36");
DATA want; SET have;
ARRAY dx_codes DX1-DX15;
do index=1 to dim(dx_codes );
IF dx_codes[index] in: (&PTB_all_list.) THEN PTB_birth_array=1; ELSE PTB_birth_array=0;
end;
IF (DX1 in (&PTB_all_list.) OR
DX2 in (&PTB_all_list.) OR
DX3 in (&PTB_all_list.) OR
DX4 in (&PTB_all_list.) OR
DX5 in (&PTB_all_list.) OR
DX6 in (&PTB_all_list.) OR
DX7 in (&PTB_all_list.) OR
DX8 in (&PTB_all_list.) OR
DX9 in (&PTB_all_list.) OR
DX10 in (&PTB_all_list.) OR
DX11 in (&PTB_all_list.) OR
DX12 in (&PTB_all_list.) OR
DX13 in (&PTB_all_list.) OR
DX14 in (&PTB_all_list.) OR
DX15 in (&PTB_all_list.)) THEN PTB_birth_line=1; ELSE PTB_birth_line=0;
RUN;
title1 "PTB";
PROC FREQ data=want;
TABLES PTB_birth_array PTB_birth_line; RUN;
And here are the frequencies:
Array code is incorrect, should be:
ARRAY dx_codes DX1-DX15;
PTB_birth_array=0;
do index=1 to dim(dx_codes );
IF dx_codes[index] in: (&PTB_all_list.) THEN PTB_birth_array=1;
end;
Array code is incorrect, should be:
ARRAY dx_codes DX1-DX15;
PTB_birth_array=0;
do index=1 to dim(dx_codes );
IF dx_codes[index] in: (&PTB_all_list.) THEN PTB_birth_array=1;
end;
An alternate:
ARRAY dx_codes DX1-DX15; PTB_birth_array=0; do index=1 to dim(dx_codes ); IF dx_codes[index] in: (&PTB_all_list.) THEN do; PTB_birth_array=1; leave; end; end;
The Leave instruction will terminate the checking after finding the first match. This might be useful as the Index value will be that of the Dx_codes array element that was first found in the list, potentially very useful if there is some order to the list.
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.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.