Search code examples
loopsspss

Loop in SPSS to draw sample repeatedly and get a descriptive statistics from each sample


I want to draw sample of size 50 for 100 times from a dataset and get a descriptive statistics from each sample in the output window. I have used the following codes, but getting warning!!!

loop # = 1 to 100.
USE ALL. 
do if $casenum=1. 
compute #s_$_1=50. 
compute #s_$_2=10251. 
end if. 
do if  #s_$_2 > 0. 
compute filter_$=uniform(1)* #s_$_2 < #s_$_1. 
compute #s_$_1=#s_$_1 - filter_$. 
compute #s_$_2=#s_$_2 - 1. 
else. 
compute filter_$=0. 
end if. 
VARIABLE LABELS filter_$ '50 from the first 10251 cases (SAMPLE)'. 
FORMATS filter_$ (f1.0). 
FILTER  BY filter_$. 
EXECUTE. 
DESCRIPTIVES VARIABLES=myvar1 
/STATISTICS=MEAN STDDEV VARIANCE SEMEAN.
end loop.
exe.    

I am getting the first warning as:

>Warning # 142.  Command name: USE 
>LOOP has no effect on this command.   

I am new in SPSS, and never did loop before.
I am using SPSS version 22.
Any help will be appreciated. Apology if it is a duplicate question.


Solution

  • One way would be to wrap the sample and descriptives commands you're trying to use in a macro:

    DEFINE !repsample(times=!TOKENS(1) / size=!TOKENS(1) / maxn = !TOKENS(1) / vars=!CMDEND)
    !DO !cnt=1 !TO !times.
    do if $casenum=1.
    compute #s_$_1=!size.
    compute #s_$_2=!maxn.
    end if.
    do if  #s_$_2 > 0.
    compute filter_$=uniform(1)* #s_$_2 < #s_$_1.
    compute #s_$_1=#s_$_1 - filter_$.
    compute #s_$_2=#s_$_2 - 1.
    else.
    compute filter_$=0.
    end if.
    FORMATS filter_$ (f1.0).
    FILTER  BY filter_$.
    DESCRIPTIVES VARIABLES= !vars
    /STATISTICS=MEAN STDDEV VARIANCE SEMEAN.
    !DOEND
    !ENDDEFINE.
    

    The macro above has four arguments - times to sample, size of sample, from first maxn of cases, and vars to use (accepts a list of variables separated by a space). We can call it like so:

    !repsample times = 100 size = 50 maxn = 10251 vars = myvar1.