SPSS transformations between DO IF ... and END IF are applied only to cases (rows of data) that satisfy one or more conditions. In many cases, IF is a faster way to accomplish the same results.
SPSS Do If Example
Say we'd like to convert people's monthly income into income classes. We may want to use different cut off points for male and female respondents. In this case, we can first use a RECODE command only for cases whose gender is female. Next, we'll use a different RECODE command for males. The syntax below demonstrates this, using employees.sav.
SPSS Do If Syntax Example 1
cd 'd:downloaded'./*Or wherever "employees.sav" is located.
get file 'employees.sav'.
*2. Recode command restricted to female respondents.
do if gender = 0.
recode monthly_income (lo thru 2000 = 1)(lo thru 2400 = 2)(lo thru hi = 3) into income_class.
end if.
*3. Recode command restricted to male respondents.
do if gender = 1.
recode monthly_income (lo thru 2500 = 1)(lo thru 3000 = 2)(lo thru hi = 3) into income_class.
end if.
*3. Inspect result.
crosstabs income_class by gender.
Else If
Although the previous syntax does its job, there's a shorter way to accomplish the exact same result: ELSE IF ... This means that the commands that follow are carried out only for cases who 1) satisfy the current condition(s) and 2) don't satisfy any of the previous conditions. The syntax below shows how to use it.
SPSS Do If Syntax Example 2
do if gender = 0.
recode monthly_income (lo thru 2000 = 1)(lo thru 2400 = 2)(lo thru hi = 3) into income_class.
else if gender = 1.
recode monthly_income (lo thru 2500 = 1)(lo thru 3000 = 2)(lo thru hi = 3) into income_class.
end if.
Else
In a similar vein to ELSE IF ... commands that follow ELSE are carried out for all cases who don't satisfy any of the previous conditions. An important thing to notice here is that ELSE does not include cases for whom previous conditions could not be evaluated due to missing values.
The final syntax example demonstrates this by creating a birth decennium variable using XDATE. Next, FREQUENCIES confirms that respondents whose birthday is unknown are not assigned to any birth decennium.
SPSS Do If Syntax Example 3
do if xdate.year(date_of_birth) lt 1960.
compute birth_decennium = 1.
else if xdate.year(date_of_birth) lt 1970.
compute birth_decennium = 2.
else if xdate.year(date_of_birth) lt 1980.
compute birth_decennium = 3.
else.
compute birth_decennium = 4.
end if.
*2. Apply value labels to birth_decennium.
value labels birth_decennium 1 '50''s' 2 '60''s' 3 '70''s' 4 '80''s'.
*3. Inspect frequency distribution for birth_decennium.
frequencies birth_decennium.
Note
Only SPSS transformation commands can be used within DO IF. These exclude most commands that generate output such as FREQUENCIES and DESCRIPTIVES. For using such commands on subsets of cases, see FILTER, SPLIT FILE and SELECT IF.
THIS TUTORIAL HAS 16 COMMENTS:
By Zahra on December 15th, 2022
Hi Everyone,
I want to compare two assessors with each other and their choice of drugs and events. So I came up with this syntax, but it gives me an error every time I run it and I cannot seem to find how to fix it.
DO IF (Arts_harm3_event_code_1 = Apotheker_harm3_event_code_1 and Arts_harm3_event_geneesmiddelcode_1_1 = Apotheker_harm3_event_geneesmiddelcode_1_1).
COMPUTE Event_GNM =0.
ELSE IF (Arts_harm3_event_code_1 NE Apotheker_harm3_event_code_1).
COMPUTE Event_GNM =1.
ELSE IF (Arts_harm3_event_code_1 = Apotheker_harm3_event_code_1 and Arts_harm3_event_geneesmiddelcode_1_1 = Apotheker_harm3_event_geneesmiddelcode_1_2).
COMPUTE Event_GNM =0.
ELSE IF (Arts_harm3_event_code_1 = Apotheker_harm3_event_code_1 and Arts_harm3_event_geneesmiddelcode_1_1 = Apotheker_harm3_event_geneesmiddelcode_1_3).
COMPUTE Event_GNM =0.
ELSE IF (Arts_harm3_event_code_1 = Apotheker_harm3_event_code_1 and Arts_harm3_event_geneesmiddelcode_1_1 = Apotheker_harm3_event_geneesmiddelcode_1_4).
COMPUTE Event_GNM =0.
ELSE IF (Arts_harm3_event_code_1 = Apotheker_harm3_event_code_1 and Arts_harm3_event_geneesmiddelcode_1_1 = Apotheker_harm3_event_geneesmiddelcode_1_5).
COMPUTE Event_GNM =0.
ELSE IF (Arts_harm3_event_code_1 = Apotheker_harm3_event_code_1 and Arts_harm3_event_geneesmiddelcode_1_1 = Apotheker_harm3_event_geneesmiddelcode_1_6).
COMPUTE Event_GNM =0.
ELSE IF (Apotheker_harm3_event_geneesmiddelcode_1_2=Arts_harm3_event_geneesmiddelcode_1_2 *OR Apotheker_harm3_event_geneesmiddelcode_1_3=Arts_harm3_event_geneesmiddelcode_1_3
*OR Apotheker_harm3_event_geneesmiddelcode_1_4= Arts_harm3_event_geneesmiddelcode_1_4 *OR Apotheker_harm3_event_geneesmiddelcode_1_5= Arts_harm3_event_geneesmiddelcode_1_5
*OR Apotheker_harm3_event_geneesmiddelcode_1_6= Arts_harm3_event_geneesmiddelcode_1_6).
COMPUTE Event_GNM =2.
END IF.
EXECUTE.
Any tips on how to solve the problem.
Kind regards,
Zahra
By Ruben Geert van den Berg on December 15th, 2022
Hi Zahra,
For one thing, try replacing *OR by OR.
Also make sure there's no empty lines between the ELSE IF lines.
But perhaps more importantly: your syntax is a genuine disaster.
Part of your problem are your crazy long variable names. The situation becomes much easier if you use short variable names. Descriptions of your variables should go into your labels, not your names.
Also note that you can use DO IF within DO IF as in
DO IF (Arts_harm3_event_code_1 = Apotheker_harm3_event_code_1).
DO IF(Arts_harm3_event_geneesmiddelcode_1_1 = Apotheker_harm3_event_geneesmiddelcode_1_1).
COMPUTE ...
END IF.
END IF.
Also note that there's an ANY function in SPSS. So the following might work if Apotheker_harm3_event_geneesmiddelcode_1_1 through Apotheker_harm3_event_geneesmiddelcode_1_6 are next to each other in your data:
DO IF (Arts_harm3_event_code_1 = Apotheker_harm3_event_code_1).
DO IF(ANY(Arts_harm3_event_geneesmiddelcode_1_1,Apotheker_harm3_event_geneesmiddelcode_1_1 TO Apotheker_harm3_event_geneesmiddelcode_1_6).
COMPUTE Event_GNM =0.
END IF.
END IF.
Hope that helps!
SPSS tutorials
By Zahra on December 19th, 2022
Hi Ruben,
Thank you for your response, it helped me understand my syntax better!
This dataset is giving me headache hahah. I was looking for the ANY function, but did not know how to use it. Your tips are very helpfull, there is just one thing I can't figure out.
DO IF (Arts_harm3_event_code_1 = Apotheker_harm3_event_code_1).
DO IF(ANY(Arts_harm3_event_geneesmiddelcode_1_1,Apotheker_harm3_event_geneesmiddelcode_1_1 TO Apotheker_harm3_event_geneesmiddelcode_1_6).
COMPUTE Event_GNM =0.
END IF.
END IF.
This works great! There is one problem I just can't solve and it drives me crazy.
Apotheker_harm3_event_geneesmiddelcode_1_1 TO Apotheker_harm3_event_geneesmiddelcode_1_6. Are next to each other in my SPSS file. The same for Arts_harm3_event_geneesmiddelcode_1_1 TO Arts_harm3_event_geneesmiddelcode_1_6.
So, within these variables the arts and apotheker were able to choose from a list of drugs. The thing is, it is possible that for Apotheker_harm3_event_geneesmiddelcode_1_2 and Apotheker_harm3_event_geneesmiddelcode_1_3 de drugs are ibuprofen and paracetamol. While Arts_harm3_event_geneesmiddelcode_1_2 and Arts_harm3_event_geneesmiddelcode_1_3 listed paracetamol and ibuprofen.
Thus, if I am comparing these two I get a discrepancy while they give the same answer.
I want to write a syntax which says:
IF Apotheker_harm3_event_geneesmiddelcode_1_1 = Arts_harm3_event_geneesmiddelcode_1_2.
compute new_var=0
IF Apotheker_harm3_event_geneesmiddelcode_1_1 NOT EQUALS Arts_harm3_event_geneesmiddelcode_1_2.
compute new_var=1
And this for all the six columns.
I hope you have a tip how to solve this problem.
Kind regards,
Zahra
By Ruben Geert van den Berg on December 20th, 2022
Hi Zahra!
First off: shorten your variable names and move the descriptions into the labels. Your syntax is unmanageable and unreadable right now.
If you've clear and unique variable labels, you could even consider using something like
RENAME VARIABLES (ALL = V001 TO V158).
(which assumes 158 variables in your data).
Regarding your question: is it that you've 2 sets of 6 variable that you want to compare regardless the order of the sets? Then you probably want to sort both sets (horizontally) first.
This is close to impossible in just SPSS but there's the SPSSINC TRANS extension which makes it much easier.
Are your variables strings or numeric?
Best regards,
Ruben
SPSS tutorials
By Zahra on December 20th, 2022
Hi Ruben,
I will change my variable names, indeed it makes it a bit hard to read the syntax with such long names.
To give a brief explanation of my datafile.
Eventually I want to calculate the kappa statistic between my raters (Artsen en apothekers), both raters linked an event (string variable) to drugs (also string variables). So I have 6 columns of drugs for apothekers and 6 columns of drugs for artsen. Now I want to compare wether they have chosen the same drugs for each case. It is possible that apotheker chose a drug in V1 and let V2 and V3 empty and chose a drug in V4 and V5, while V6 was left empty as well. It is also possible that the other rater, arts, chose the same drugs but placed them in V2, V3 and V6 while he left V4,V1 and V5 empty. So if I compare each column separately I will find a discrepancy while actually they have chosen the same drugs. Note, that V1-V6 for artsen are placed next to each other and V1-V6 for apothekers are also placed next to each other in the same dataset.
To calculate the kappa I wanted to make a Crosstab for V1 arts and V1 apotheker, and also perform a bootstrap. Unfortunately the bootstrap does not work because SPSS says that some bootstrap sample results are missing. I don't understand why, because everything is filled in with either a drug or the word "empty" is written. I did the same for event code (string variable), all 1936 cases have an event code for example like M091AA which means gastro-intestinal bleeding. So no event codes are missing, but with the analysis of bootstrap performed through Crosstabs I got the same message. I got the output for the symmetric measures, but not the bootstrap for symmetric measures.
I hope I explained it better and I would really appreciate it if you have any tips how to calculate this. My Idea was:
DO IF arts_V1=apot_V1
compute Drug_verg=0.
DO IF arts_V1=apot_V2
compute Drug_verg=0.
DO IF arts_V1=apot_V3
compute Drug_verg=0.
etc.
until arts_V1=apot_6
compute Drug_verg=0
END IF.
END IF.
END IF.
and then I wanted to do the same but for if they did not chose the same drug.
DO IF arts_V1 NOT EQUALS apot_V1
compute Drug_verg=1.
again do this for all the 6 variables compared. Eventually I want to end up with a new variable with 0= same drugs chosen and 1= there was a difference in their drug choice. The place of the drug does not matter in this case.
Kind regards,
Zahra (a stressed student).
Ps: thank you so much for your time, I really don't know how to solve this issue.