Date: Tue, 2 Nov 2004 22:52:36 -0500
Reply-To: Richard Ristow <wrristow@mindspring.com>
Sender: "SPSSX(r) Discussion" <SPSSX-L@LISTSERV.UGA.EDU>
From: Richard Ristow <wrristow@mindspring.com>
Subject: Re: Selecting random samples of size 6 out of 49
In-Reply-To: <200411012035.iA1KZ6L1011149@myra.general.services.metu.edu .tr>
Content-Type: text/plain; charset="us-ascii"; format=flowed
At 03:35 PM 11/1/2004, ibrahim ERKAN wrote:
>I want to select 50 random samples of size 6, out of ordered 49
>numbers
>(from 1 to 49). Could you please help me do this by using syntax.
Here's one way to do it. It uses a simple random sort; there may well
be more efficient ways. All 50 samples are in the same file, marked by
SMPL_NUM running from 1 to 50. This is tested; SPSS draft output.
"SAMPLES" is a file handle for a scratch .SAV file.
NEW FILE.
INPUT PROGRAM.
NUMERIC KEY_NMBR (F3).
STRING DATA_STR (A21).
LOOP KEY_NMBR = 1 TO 49.
. COMPUTE DATA_STR = CONCAT('Test record ',
STRING(KEY_NMBR,N2),
' of 49.').
. END CASE.
END LOOP.
END FILE.
END INPUT PROGRAM.
/**/ LIST /*-*/.
List
KEY_NMBR DATA_STR
1 Test record 01 of 49.
2 Test record 02 of 49.
3 Test record 03 of 49.
[etc.]
Number of cases read: 49 Number of cases listed: 49
* Initialize the random-number generator .
SET SEED=12345.
DO IF $CASENUM = 1.
. LOOP #I = 1 TO 20.
. COMPUTE #DISCARD = RV.UNIFORM(0,1).
. END LOOP.
END IF.
* Write each record 50 times, with a different random key .
NUMERIC SMPL_NUM (F3).
VAR LABEL SMPL_NUM 'Sample, of the 50'.
NUMERIC RAND_KEY (F5).
VAR LABEL RAND_KEY 'Random sort key, within sample'.
LOOP SMPL_NUM = 1 TO 50.
. COMPUTE RAND_KEY = RV.UNIFORM(0,1E5-1).
. XSAVE OUTFILE=SAMPLES
/KEEP = SMPL_NUM RAND_KEY ALL.
END LOOP.
EXECUTE.
* Recover the 50 copies of each record, and random-sort .
GET FILE=SAMPLES.
/**/ LIST /CASES=05 /*-*/.
List
SMPL_NUM RAND_KEY KEY_NMBR DATA_STR
1 28021 1 Test record 01 of 49.
2 56992 1 Test record 01 of 49.
3 35052 1 Test record 01 of 49.
4 80016 1 Test record 01 of 49.
5 61896 1 Test record 01 of 49.
Number of cases read: 5 Number of cases listed: 5
SORT CASES BY SMPL_NUM RAND_KEY.
/**/ LIST /CASES=05 /*-*/.
List
SMPL_NUM RAND_KEY KEY_NMBR DATA_STR
1 4244 48 Test record 48 of 49.
1 4388 13 Test record 13 of 49.
1 4623 32 Test record 32 of 49.
1 5816 15 Test record 15 of 49.
1 6078 19 Test record 19 of 49.
Number of cases read: 5 Number of cases listed: 5
* Select the first 6 members of each sample .
DO IF MISSING(LAG(SMPL_NUM)).
. COMPUTE #IN_SAMP = 1.
ELSE IF SMPL_NUM > LAG(SMPL_NUM).
. COMPUTE #IN_SAMP = 1.
ELSE.
. COMPUTE #IN_SAMP = #IN_SAMP + 1.
END IF.
SELECT IF #IN_SAMP LE 6.
/**/ LIST /CASES=20 /*-*/.
List
SMPL_NUM RAND_KEY KEY_NMBR DATA_STR
1 4244 48 Test record 48 of 49.
1 4388 13 Test record 13 of 49.
1 4623 32 Test record 32 of 49.
1 5816 15 Test record 15 of 49.
1 6078 19 Test record 19 of 49.
1 6408 49 Test record 49 of 49.
2 5131 23 Test record 23 of 49.
2 6628 34 Test record 34 of 49.
2 8792 30 Test record 30 of 49.
2 9268 47 Test record 47 of 49.
2 10001 18 Test record 18 of 49.
2 12451 36 Test record 36 of 49.
3 530 21 Test record 21 of 49.
3 4984 31 Test record 31 of 49.
3 14148 9 Test record 09 of 49.
3 16232 11 Test record 11 of 49.
3 16348 27 Test record 27 of 49.
3 22208 34 Test record 34 of 49.
4 3397 48 Test record 48 of 49.
4 5803 16 Test record 16 of 49.
Number of cases read: 20 Number of cases listed: 20
* Select the first 6 members of each sample .
* If desired, put records in original order in each sample .
SORT CASES BY SMPL_NUM KEY_NMBR.
/**/ LIST /CASES=20 /*-*/.
List
SMPL_NUM RAND_KEY KEY_NMBR DATA_STR
1 4388 13 Test record 13 of 49.
1 5816 15 Test record 15 of 49.
1 6078 19 Test record 19 of 49.
1 4623 32 Test record 32 of 49.
1 4244 48 Test record 48 of 49.
1 6408 49 Test record 49 of 49.
2 10001 18 Test record 18 of 49.
2 5131 23 Test record 23 of 49.
2 8792 30 Test record 30 of 49.
2 6628 34 Test record 34 of 49.
2 12451 36 Test record 36 of 49.
2 9268 47 Test record 47 of 49.
3 14148 9 Test record 09 of 49.
3 16232 11 Test record 11 of 49.
3 530 21 Test record 21 of 49.
3 16348 27 Test record 27 of 49.
3 4984 31 Test record 31 of 49.
3 22208 34 Test record 34 of 49.
4 5803 16 Test record 16 of 49.
4 8376 21 Test record 21 of 49.
Number of cases read: 20 Number of cases listed: 20