Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting cases using macro in SPSS

Tags:

case

spss

I am trying to select cases which have specific diagnosis codes. Diagnosis codes are stored in multiple columns DX1, DX2, DX3, DX4 .....etc' for each patient. Each patient could have multiple diagnosis. If the ICD code 75673 matches in any of the diagnosis column that means that that case (patient) had specific diagnsis-in this case gastroschisis and GastroID should be recorded as 1, other wise GastroID should be 0.

Sometimes ICD codes are in range and each diagnosis column needs to be checked against one of the ICD code in the range. For example Abdominal wall defects consist of codes between 7567 to 75679.

I tried to loop as under. it creates the column name GastroID but the output is not "1" or "0". What I may be doing wrong?

DEFINE FINDCASES (ICD=!CHAREND("/") /
                            DX=!CMDEND).
!DO !I !IN (!ICD)
!DO !J !IN (!DX)
!IF (!J = !I) !THEN COMPUTE GastroID=1
!ELSE COMPUTE GastroID=0
!IFEND
!DOEND
!DOEND
!ENDDEFINE.
FINDCASES ICD = '75673' /DX=DX1 DX2 DX3 DX4.
like image 683
Jaimin Avatar asked Feb 27 '26 18:02

Jaimin


2 Answers

This is a loop that uses the basic idea in @horace_vr's answer but solves multiple conditions:

compute GastroID=0.
do repeat CodNum=7567 75679 75673.
   if GastroID=0 GastroID=any(CodNum, DX1 to DX4).
end repeat.

Note - if variables DX1 to DX4 are not consecutive in the file, you can not use to in the syntax but would have to name them each separately. Also, as @horace_vr says, if these variables are text and not numbers, put the numbers in quotation marks.

Now if you have a number of code groups like this one, you could create a do repeat loop like the one above for each. Or you can use one loop (tho this may be a bit confusing):

numeric GastroID grp2ID grp3ID (f1).
do repeat CodNum=7567 75679 75673 111 112 113 456 4567 4566
  /NewVar=GastroID GastroID GastroID grp2ID grp2ID grp2ID grp3ID grp3ID grp3ID.
   if missing(NewVar) or NewVar=0 NewVar=any(CodNum, DX1 to DX4).
end repeat.
like image 142
eli-k Avatar answered Mar 01 '26 19:03

eli-k


You do not need macros. If you have to check 1 code against a set of variables:

numeric GastroID (f1.0).
compute GastroID=0.
if any(75673 ,DX1, DX2, DX3, DX4) GastroID=1.
execute.

It is not clear from your post If DX1 - DX4 are string or numerical variables. If they are string, you will need to pass the argument(s) as string, so use:

if any('75673' ,DX1, DX2, DX3, DX4) GastroID=1.

instead of the above.

If you have to check multiple codes against a set of variables, you could use count

count GastroID=DX1 DX2 DX3 DX4 (code1, code2, code3, code4).
exe.
if GastroID>1 GastriID=1.
exe.

if you have a range of codes, you can also write it as:

count GastroID=DX1 DX2 DX3 DX4 (code1 thru code4).

but: - make sure you need all numbers between code1 and code4 - this is risky with strings, because string ranges work in a different way; they are sorted by the first character; so "2">"10"; therefore "1" to "10" includes all stringswhich start with "2"

Are all your DXs numbers stored as strings ? If you are sure this is the case, you might want to transform them all into numerical variables:

alter type variable_name (f8.0).

Numbers are much easier to process than strings.

like image 23
horace_vr Avatar answered Mar 01 '26 18:03

horace_vr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!