## Using Median and Standard Deviation in syntax

Moderators: statman, Analyst Techy, andris, Fierce, GerineL, Smash

JW22
Posts: 1
Joined: Tue Mar 18, 2014 7:37 pm

### Using Median and Standard Deviation in syntax

Hi,

I'm quite new to SPSS and am looking to code my data records by whether or not they are within 1 standard deviation of the median for that variable.

So far I have been doing this for each variable:

Code: Select all

``````DO IF (HumanIL21Normalised GE -1.6336 AND HumanIL21Normalised LE 0.3664).
COMPUTE OutsideSD = 1.
ELSE.
COMPUTE InsideSD = 0.
END IF.``````
Here, (Median of HumanIL21Normalised - 1 Standard Deviation of HumanIL21Normalised) = '-1.6336' and the other value is + 1 Standard Deviation.

I want to have the syntax be able to calculate the values here without me having to manually work out the median and standard deviation. Is there a way to do this?

The purpose of this is so that I can split data into high and low groups after removing records within one standard deviation of the median.

Thanks!
GerineL
Moderator
Posts: 1477
Joined: Tue Jun 10, 2008 4:50 pm

### Re: Using Median and Standard Deviation in syntax

I use a do repeat like this (alter with your vars / what you want of course):

AGGREGATE
/BREAK=
/totcorrect_angry_mean=MEAN(totcorrect_angry)
/totcorrect_fear_mean=MEAN(totcorrect_fear)
/totcorrect_happy_mean=MEAN(totcorrect_happy)
/totspeed_angry_mean=MEAN(totspeed_angry)
/totspeed_fear_mean=MEAN(totspeed_fear)
/totspeed_happy_mean=MEAN(totspeed_happy)
/speed_true_angry_mean=MEAN(speed_true_angry)
/speed_true_fear_mean=MEAN(speed_true_fear)
/speed_false_angry_mean=MEAN(speed_false_angry)
/speed_false_fear_mean=MEAN(speed_false_fear)
/speed_true_happy_mean=MEAN(speed_true_happy)
/speed_false_happy_mean=MEAN(speed_false_happy)
/angry_fast_mean=MEAN(angry_fast)
/angry_slow_mean=MEAN(angry_slow)
/contempt_fast_mean=MEAN(contempt_fast)
/contempt_slow_mean=MEAN(contempt_slow)
/disgust_fast_mean=MEAN(disgust_fast)
/disgust_slow_mean=MEAN(disgust_slow)
/fear_fast_mean=MEAN(fear_fast)
/fear_slow_mean=MEAN(fear_slow)
/happy_fast_mean=MEAN(happy_fast)
/happy_slow_mean=MEAN(happy_slow)
/surprise_fast_mean=MEAN(surprise_fast)
/surprise_slow_mean=MEAN(surprise_slow)
/cesd_tot_mean=MEAN(cesd_tot)
/NTB_tot_mean=MEAN(NTB_tot)
/leka_tot_mean=MEAN(leka_tot)
/se_tot_mean=MEAN(se_tot)
/positiveaf_tot_mean=MEAN(positiveaf_tot)
/negativeaf_tot_mean=MEAN(negativeaf_tot)
/epq_n_mean=MEAN(epq_n)
/epq_e_mean=MEAN(epq_e).

AGGREGATE
/BREAK=
/totcorrect_angry_sd=SD(totcorrect_angry)
/totcorrect_fear_sd=SD(totcorrect_fear)
/totcorrect_happy_sd=SD(totcorrect_happy)
/totspeed_angry_sd=SD(totspeed_angry)
/totspeed_fear_sd=SD(totspeed_fear)
/totspeed_happy_sd=SD(totspeed_happy)
/speed_true_angry_sd=SD(speed_true_angry)
/speed_true_fear_sd=SD(speed_true_fear)
/speed_false_angry_sd=SD(speed_false_angry)
/speed_false_fear_sd=SD(speed_false_fear)
/speed_true_happy_sd=SD(speed_true_happy)
/speed_false_happy_sd=SD(speed_false_happy)
/angry_fast_sd=SD(angry_fast)
/angry_slow_sd=SD(angry_slow)
/contempt_fast_sd=SD(contempt_fast)
/contempt_slow_sd=SD(contempt_slow)
/disgust_fast_sd=SD(disgust_fast)
/disgust_slow_sd=SD(disgust_slow)
/fear_fast_sd=SD(fear_fast)
/fear_slow_sd=SD(fear_slow)
/happy_fast_sd=SD(happy_fast)
/happy_slow_sd=SD(happy_slow)
/surprise_fast_sd=SD(surprise_fast)
/surprise_slow_sd=SD(surprise_slow)
/cesd_tot_sd=SD(cesd_tot)
/NTB_tot_sd=SD(NTB_tot)
/leka_tot_sd=SD(leka_tot)
/se_tot_sd=SD(se_tot)
/positiveaf_tot_sd=SD(positiveaf_tot)
/negativeaf_tot_sd=SD(negativeaf_tot)
/epq_n_sd=SD(epq_n)
/epq_e_sd=SD(epq_e).

do repeat means =totcorrect_angry_mean totcorrect_sad_mean totcorrect_fear_mean
angry_fast_mean angry_slow_mean contempt_fast_mean contempt_slow_mean disgust_fast_mean
disgust_slow_mean fear_fast_mean fear_slow_mean happy_fast_mean happy_slow_mean sad_fast_mean
sad_slow_mean surprise_fast_mean surprise_slow_mean cesd_tot_mean NTB_tot_mean leka_tot_mean
se_tot_mean positiveaf_tot_mean negativeaf_tot_mean epq_n_mean epq_e_mean /
sds = totcorrect_angry_sd
speed_false_happy_sd angry_fast_sd angry_slow_sd contempt_fast_sd contempt_slow_sd disgust_fast_sd
surprise_fast_sd surprise_slow_sd cesd_tot_sd NTB_tot_sd leka_tot_sd se_tot_sd positiveaf_tot_sd
negativeaf_tot_sd epq_n_sd epq_e_sd/
angry_fast_plustwos angry_slow_plustwos contempt_fast_plustwos contempt_slow_plustwos disgust_fast_plustwos
disgust_slow_plustwos fear_fast_plustwos fear_slow_plustwos happy_fast_plustwos happy_slow_plustwos sad_fast_plustwos
sad_slow_plustwos surprise_fast_plustwos surprise_slow_plustwos cesd_tot_plustwos NTB_tot_plustwos leka_tot_plustwos
se_tot_plustwos positiveaf_tot_plustwos negativeaf_tot_plustwos epq_n_plustwos epq_e_plustwos /
angry_fast_mintwos angry_slow_mintwos contempt_fast_mintwos contempt_slow_mintwos disgust_fast_mintwos
disgust_slow_mintwos fear_fast_mintwos fear_slow_mintwos happy_fast_mintwos happy_slow_mintwos sad_fast_mintwos
sad_slow_mintwos surprise_fast_mintwos surprise_slow_mintwos cesd_tot_mintwos NTB_tot_mintwos leka_tot_mintwos
se_tot_mintwos positiveaf_tot_mintwos negativeaf_tot_mintwos epq_n_mintwos epq_e_mintwos .
compute plustwos= means + (2*sds).
compute mintwos = means - (2*sds).
end repeat.
execute.

do repeat plustwos = totcorrect_angry_plustwos totcorrect_sad_plustwos totcorrect_fear_plustwos
angry_fast_plustwos angry_slow_plustwos contempt_fast_plustwos contempt_slow_plustwos disgust_fast_plustwos
disgust_slow_plustwos fear_fast_plustwos fear_slow_plustwos happy_fast_plustwos happy_slow_plustwos sad_fast_plustwos
sad_slow_plustwos surprise_fast_plustwos surprise_slow_plustwos cesd_tot_plustwos NTB_tot_plustwos leka_tot_plustwos
se_tot_plustwos positiveaf_tot_plustwos negativeaf_tot_plustwos epq_n_plustwos epq_e_plustwos /
angry_fast_mintwos angry_slow_mintwos contempt_fast_mintwos contempt_slow_mintwos disgust_fast_mintwos
disgust_slow_mintwos fear_fast_mintwos fear_slow_mintwos happy_fast_mintwos happy_slow_mintwos sad_fast_mintwos
sad_slow_mintwos surprise_fast_mintwos surprise_slow_mintwos cesd_tot_mintwos NTB_tot_mintwos leka_tot_mintwos
se_tot_mintwos positiveaf_tot_mintwos negativeaf_tot_mintwos epq_n_mintwos epq_e_mintwos /
angry_fast angry_slow contempt_fast contempt_slow disgust_fast
disgust_slow fear_fast fear_slow happy_fast happy_slow sad_fast
sad_slow surprise_fast surprise_slow cesd_tot NTB_tot leka_tot
se_tot positiveaf_tot negativeaf_tot epq_n epq_e /
angry_fast_temp angry_slow_temp contempt_fast_temp contempt_slow_temp disgust_fast_temp
disgust_slow_temp fear_fast_temp fear_slow_temp happy_fast_temp happy_slow_temp sad_fast_temp
sad_slow_temp surprise_fast_temp surprise_slow_temp cesd_tot_temp NTB_tot_temp leka_tot_temp
se_tot_temp positiveaf_tot_temp negativeaf_tot_temp epq_n_temp epq_e_temp .
compute newor = originals.
if (originals < mintwos) newor = mintwos.
if (originals > plustwos) newor = plustwos.
end repeat.
execute.

do repeat originals = totcorrect_angry totcorrect_sad totcorrect_fear
angry_fast angry_slow contempt_fast contempt_slow disgust_fast
disgust_slow fear_fast fear_slow happy_fast happy_slow sad_fast
sad_slow surprise_fast surprise_slow cesd_tot NTB_tot leka_tot
se_tot positiveaf_tot negativeaf_tot epq_n epq_e /
angry_fast_temp angry_slow_temp contempt_fast_temp contempt_slow_temp disgust_fast_temp
disgust_slow_temp fear_fast_temp fear_slow_temp happy_fast_temp happy_slow_temp sad_fast_temp
sad_slow_temp surprise_fast_temp surprise_slow_temp cesd_tot_temp NTB_tot_temp leka_tot_temp
se_tot_temp positiveaf_tot_temp negativeaf_tot_temp epq_n_temp epq_e_temp/
angry_fast_diff angry_slow_diff contempt_fast_diff contempt_slow_diff disgust_fast_diff
disgust_slow_diff fear_fast_diff fear_slow_diff happy_fast_diff happy_slow_diff sad_fast_diff
sad_slow_diff surprise_fast_diff surprise_slow_diff cesd_tot_diff NTB_tot_diff leka_tot_diff
se_tot_diff positiveaf_tot_diff negativeaf_tot_diff epq_n_diff epq_e_diff .
compute diff = originals - newor.
end repeat.
execute.

* aantal outliers .

COUNT microoutlier=angry_fast_diff angry_slow_diff contempt_fast_diff contempt_slow_diff
disgust_fast_diff disgust_slow_diff fear_fast_diff fear_slow_diff happy_fast_diff happy_slow_diff
angry_fast_diff angry_slow_diff contempt_fast_diff contempt_slow_diff disgust_fast_diff
disgust_slow_diff fear_fast_diff fear_slow_diff happy_fast_diff happy_slow_diff sad_fast_diff
VARIABLE LABELS microoutlier 'outliers microtaak'.
EXECUTE.

speed_false_happy_diff(Lowest thru -0.00000001).
VARIABLE LABELS morphoutliers 'outliers microtaak'.
EXECUTE.

COUNT vragenlijstoutliers=cesd_tot_diff NTB_tot_diff leka_tot_diff se_tot_diff positiveaf_tot_diff
negativeaf_tot_diff epq_n_diff epq_e_diff(0.00000001 thru Highest) cesd_tot_diff NTB_tot_diff
leka_tot_diff se_tot_diff positiveaf_tot_diff negativeaf_tot_diff epq_n_diff epq_e_diff(Lowest thru
-0.00000001).
VARIABLE LABELS vragenlijstoutliers 'outliers microtaak'.
EXECUTE.

compute outlierstotal = microoutlier + morphoutliers + vragenlijstoutliers.
execute.

* variableen vervangen e.d.

angry_fast_mean angry_slow_mean contempt_fast_mean contempt_slow_mean disgust_fast_mean
disgust_slow_mean fear_fast_mean fear_slow_mean happy_fast_mean happy_slow_mean sad_fast_mean
sad_slow_mean surprise_fast_mean surprise_slow_mean cesd_tot_mean NTB_tot_mean leka_tot_mean
se_tot_mean positiveaf_tot_mean negativeaf_tot_mean epq_n_mean epq_e_mean totcorrect_angry_sd
speed_false_happy_sd angry_fast_sd angry_slow_sd contempt_fast_sd contempt_slow_sd disgust_fast_sd
surprise_fast_sd surprise_slow_sd cesd_tot_sd NTB_tot_sd leka_tot_sd se_tot_sd positiveaf_tot_sd
negativeaf_tot_sd epq_n_sd epq_e_sd totcorrect_angry_plustwos totcorrect_angry_mintwos
totcorrect_happy_plustwos totcorrect_happy_mintwos totspeed_angry_plustwos totspeed_angry_mintwos
totspeed_happy_plustwos totspeed_happy_mintwos speed_true_angry_plustwos speed_true_angry_mintwos
speed_true_happy_plustwos speed_true_happy_mintwos speed_false_happy_plustwos
speed_false_happy_mintwos angry_fast_plustwos angry_fast_mintwos angry_slow_plustwos
angry_slow_mintwos contempt_fast_plustwos contempt_fast_mintwos contempt_slow_plustwos
contempt_slow_mintwos disgust_fast_plustwos disgust_fast_mintwos disgust_slow_plustwos
disgust_slow_mintwos fear_fast_plustwos fear_fast_mintwos fear_slow_plustwos fear_slow_mintwos
surprise_slow_plustwos surprise_slow_mintwos cesd_tot_plustwos cesd_tot_mintwos NTB_tot_plustwos
NTB_tot_mintwos leka_tot_plustwos leka_tot_mintwos se_tot_plustwos se_tot_mintwos
positiveaf_tot_plustwos positiveaf_tot_mintwos negativeaf_tot_plustwos negativeaf_tot_mintwos
speed_false_happy_diff angry_fast_diff angry_slow_diff contempt_fast_diff contempt_slow_diff
disgust_fast_diff disgust_slow_diff fear_fast_diff fear_slow_diff happy_fast_diff happy_slow_diff
leka_tot_diff se_tot_diff positiveaf_tot_diff negativeaf_tot_diff epq_n_diff epq_e_diff
totcorrect_angry_diff cesd_tot_temp NTB_tot_temp
leka_tot_temp se_tot_temp positiveaf_tot_temp negativeaf_tot_temp epq_e_temp .
execute.

delete variables totcorrect_angry totcorrect_sad totcorrect_fear totcorrect_happy
speed_false_fear speed_true_happy speed_false_happy angry_fast
angry_slow contempt_fast contempt_slow disgust_fast disgust_slow

rename variables
( totcorrect_angry_temp = totcorrect_angry )
( totcorrect_fear_temp = totcorrect_fear )
( totcorrect_happy_temp = totcorrect_happy )
( totspeed_angry_temp = totspeed_angry )
( totspeed_fear_temp = totspeed_fear )
( totspeed_happy_temp = totspeed_happy )
( speed_true_angry_temp = speed_true_angry )
( speed_true_fear_temp = speed_true_fear )
( speed_false_angry_temp = speed_false_angry )
( speed_false_fear_temp = speed_false_fear )
( speed_true_happy_temp = speed_true_happy )
( speed_false_happy_temp = speed_false_happy )
( angry_fast_temp = angry_fast )
( angry_slow_temp = angry_slow )
( contempt_fast_temp = contempt_fast )
( contempt_slow_temp = contempt_slow )
( disgust_fast_temp = disgust_fast )
( disgust_slow_temp = disgust_slow )
( fear_fast_temp = fear_fast )
( fear_slow_temp = fear_slow )
( happy_fast_temp = happy_fast )
( happy_slow_temp = happy_slow )