'Topic_17.sbs 'Calculates the means of specified samples 'Written by M Miller for Mth 110 and Mth 111 at Le Moyne 'Last revised 27-Oct-2006 Sub main Sample_Size = 1 Number_of_Samples = 200 Variable = "age" Set Document = objSpssApp.Documents Set DataDoc = Document.GetDataDoc(0) Population_Size = DataDoc.GetNumberOfCases 'This is a kludge to create the variable SampleMeans. objSpssApp.ExecuteCommands "Compute SampleMeans = 0.", True objSpssApp.ExecuteCommands "Execute.", True DataDoc.SelectCells("SampleMeans", "SampleMeans", 1, Population_Size) DataDoc.Clear 'Read in the values of Variable Values = DataDoc.GetTextData (Variable, Variable, 1, Population_Size) 'This array tracks of the values used, so they don't get re-sampled. Dim Used() ReDim Used (0 To Population_Size) Used(0) = 1 'If Rnd=0 then CInt rounds 0.5 to 0 so K=0; 'this excludes that (very rare) case. For I = 1 To Number_of_Samples For J = 1 To Population_Size Used(J) = 0 Next J 'Calculate the mean of a random sample, sampling without replacement. Sum = 0 For J = 1 To Sample_Size K = 0 While Used(K) = 1 K = CInt(Population_Size*Rnd + 0.5) 'Rounds to nearest integer. Wend Used(K) = 1 Sum = Sum + CSng(Values(0, K-1)) 'Values is a 0-based array. Next J Mean = Sum/Sample_Size 'Copy the mean to the clipboard, then paste into the ith cell. Clipboard CStr(Mean) DataDoc.SelectCells ("SampleMeans", "SampleMeans", i, i) DataDoc.Paste Next I End Sub