Trouble trying to loop to create 1000 random samples.

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

zt644
Posts: 1
Joined: Thu Jul 12, 2012 3:44 am

Trouble trying to loop to create 1000 random samples.

Postby zt644 » Sat Jul 14, 2012 7:14 pm

I'm new to SPSS coding, and I am trying to take 1000 random samples, of 10 from 60, and I'm fine with blocking the codes for each of the 1000 times I have to do it. I have tried to loop the code but keep failing and am getting frustrated. My code example is below:

DATASET ACTIVATE DataSet1.
DATASET COPY T10100_S0001.
DATASET ACTIVATE T10100_S0001.
FILTER OFF.
USE ALL.
SAMPLE 10 from 60.
EXECUTE.
DATASET ACTIVATE DataSet1.
DATASET ACTIVATE T10100_S0001.
SAVE TRANSLATE OUTFILE = 'C:\T10100\S1.dbf'
/TYPE = DBF
/VERSION = 4
/MAP
/REPLACE.

DATASET ACTIVATE DataSet1.
DATASET COPY T10100_S0002.
DATASET ACTIVATE T10100_S0002.
FILTER OFF.
USE ALL.
SAMPLE 10 from 60.
EXECUTE.
DATASET ACTIVATE DataSet1.
DATASET ACTIVATE T10100_S0002.
SAVE TRANSLATE OUTFILE = 'C:\T10100\S2.dbf'
/TYPE = DBF
/VERSION = 4
/MAP
/REPLACE.

DATASET ACTIVATE DataSet1.
DATASET COPY T10100_S0003.
DATASET ACTIVATE T10100_S0003.
FILTER OFF.
USE ALL.
SAMPLE 10 from 60.
EXECUTE.
DATASET ACTIVATE DataSet1.
DATASET ACTIVATE T10100_S0003.
SAVE TRANSLATE OUTFILE = 'C:\T10100\S3.dbf'
/TYPE = DBF
/VERSION = 4
/MAP
/REPLACE.

I need the sample number (for example sample 1, to end up with the name S1, sample 2, with the name S2.dbf, etc..)

I cannot figure out how to have it recognize the loop number as a string to attach to the end of S in the .dbf file name. There's a CONCAT function, but I can't figure out how to use that either.

Hopefully somebody can help. This doesn't seem like it's too far away, a few lines, and it should be good to go.. :cry:
Penguin_Knight
Posts: 473
Joined: Thu Apr 05, 2012 5:58 pm

Re: Trouble trying to loop to create 1000 random samples.

Postby Penguin_Knight » Mon Jul 16, 2012 12:29 pm

OMG, don't copy and paste 1000 blocks. Use macro, here is a start:

Code: Select all

DEFINE ThousandFiles ()
!DO !I = 1 !TO 1000 
GET
  FILE='C:\Program Files\SPSSInc\PASWStatistics17\Samples\English\1991 U.S. General Social Survey.sav'.
DATASET NAME DataSet1 WINDOW=FRONT.
DATASET ACTIVATE DataSet1.
FILTER OFF.
USE ALL.
SAMPLE  10 from 60.
EXECUTE.
SAVE TRANSLATE OUTFILE=!QUOTE(!CONCAT("C:\Temp\s", !I, ".dbf"))
  /TYPE=DBF
  /VERSION=4
  /MAP
  /REPLACE.
!DOEND
!ENDDEFINE .

ThousandFiles .
EXECUTE .
1) Replace "!DO !I = 1 !TO 1000" to something smaller, like "!DO !I = 1 !TO 10" first before making sure the syntax is right.
2) Change this file: FILE='C:\Program Files\SPSSInc\PASWStatistics17\Samples\English\1991 U.S. General Social Survey.sav'. to your own designated file.
3) Change the directory path: !QUOTE(!CONCAT("C:\Temp\s", !I, ".dbf")) to your designated path.

There will be some error messages telling that the name DataSet1 has been assigned, you can ignore them.

Who is online

Users browsing this forum: No registered users and 1 guest

cron