Date: Sat, 16 Jun 2001 10:06:16 -0400
Reply-To: Raynald Levesque <rlevesque@VIDEOTRON.CA>
Sender: "SPSSX(r) Discussion" <SPSSX-L@LISTSERV.UGA.EDU>
From: Raynald Levesque <rlevesque@VIDEOTRON.CA>
Subject: Re: Using a macro to write data to multiple text files.
In-Reply-To: <LDEEKLGKNMECOPKLIGJNEEGOCDAA.rlevesque@videotron.ca>
Content-Type: text/plain; charset="us-ascii"
Hi gang,
The following is a more general approach to split a file into many files
based on the various values of a categorical variable.
http://pages.infinit.net/rlevesqu/Syntax/WorkingWithManyFiles/SplitBigFilesI
ntoSeparateCategories.txt
HTH
Raynald Levesque rlevesque@videotron.ca
Visit My SPSS Pages: http://pages.infinit.net/rlevesqu/
-----Original Message-----
From: SPSSX(r) Discussion [mailto:SPSSX-L@LISTSERV.UGA.EDU]On Behalf Of
Raynald Levesque
Sent: Friday, June 15, 2001 10:25 PM
To: SPSSX-L@LISTSERV.UGA.EDU
Subject: Re: Using a macro to write data to multiple text files.
Hi Frank
This does it:
* Define some dummy data to illustrate the solution *.
INPUT PROGRAM.
NUMERIC iyeartp (F8.0).
LOOP #yr=1996 TO 2006 BY 5.
LOOP #peri=1 TO 4.
LOOP #casenb=1 TO 5.
COMPUTE iyeartp=#yr*10+#peri.
COMPUTE var1=RND(UNIFORM(100)).
COMPUTE var2=RND(UNIFORM(100)).
COMPUTE var3=RND(UNIFORM(100)).
END CASE.
END LOOP.
END LOOP.
END LOOP.
END FILE.
END INPUT PROGRAM.
LIST.
SAVE OUTFILE='c:\temp\mydata.sav'.
* Now start the job *.
SET MPRINT=yes.
DEFINE !mydata()'c:\temp\mydata.sav'!enddefine.
DEFINE !outdir()'c:\temp\'!enddefine.
*//////////////////.
DEFINE WR_SCEN (!POS=!TOKENS(1) /!POS=!TOKENS(1))
!DO !yr = !1 !TO !2 !BY 5
!DO !peri=1 !TO 4
GET FILE = !mydata.
SELECT IF iyeartp = !CONCAT(!yr,!peri).
* Define output file name.
!LET
!OUTFILE=!QUOTE(!CONCAT(!UNQUOTE(!EVAL(!outdir)),'file',!yr,!peri,'.txt'))
WRITE OUTFILE=!OUTFILE / iyeartp, var1, var2, var3.
DESCRIPTIVE VARS = all / STATISTICS = DEFAULT SUM.
!DOEND
!DOEND
!ENDDEFINE.
*//////////////////.
* Now actually write out the scenario data.
WR_SCEN 1996 2001.
Raynald Levesque rlevesque@videotron.ca
Visit My SPSS Pages: http://pages.infinit.net/rlevesqu/
-----Original Message-----
From: SPSSX(r) Discussion [mailto:SPSSX-L@LISTSERV.UGA.EDU]On Behalf Of
Frank Milthorpe
Sent: Friday, June 15, 2001 1:24 AM
To: SPSSX-L@LISTSERV.UGA.EDU
Subject: Using a macro to write data to multiple text files.
Hi
I have been trying to write text (data) to multiple files using SPSS
syntax for sue by other an application. I have been using a macro
with only partial success.
Within my database (save file) I have data for a number of years 1996,
2001, 2006 and etc. Within each year the data is segmented into 4 time
periods. I have a variable YEARTP on the file with the values 19961,
19962, 19963, 19964, 20011, 20012, 20013, 20014 and etc. Using the
AUTORECODE command I have been able to create unique values for each
YEARTP in the variable IYEARTP which has the values 1,2,3,4, ...
I would like the data for each unique value of YEARTP (IYEARTP) to be
written to separate files 19961.txt, 19962.txt and etc.
I have had partial success using a macro. I have been able to write to
unique files with the name file1.txt, file2.txt and etc using the auto-
recoded variable value.
Hence is a brief overview of my macro.
DEFINE !mydata ( ) 'mydata.sav' !enddefine.
DEFINE !outdir ( ) '..\results\' !enddefine.
DEFINE WR_SCEN ( ).
. !DO !ifile = 1 !TO 32.
. GET FILE = !mydata.
. SELECT IF (iyeartp = !ifile).
. * Define output file name.
. !LET !OUTFILE = !QUOTE( !CONCAT(
!UNQUOTE(!EVAL(!outdir)),'file',!ifile,'.txt') ).
. WRITE OUTFILE = !OUTFILE / iyeartp, var1, var2, var3.
. DESCRIPTIVE VARS = all / STATISTICS = DEFAULT SUM.
. !DOEND.
!ENDDEFINE.
* Now actually write out the scenario data.
WR_SCEN.
I have 32 unique values of IYEARTP. I have tried to using the !LET
statement, but I did not seem to be able to use values from the data.
Also with the !DO command it does to seem to possible to hard-code the
different values unlike the DO REPEAT command.
By way of interest, I renamed the files using a simple DOS batch file
with RENAME file1.txt 19961.txt and etc.
I can use a SPLIT FILE command to avoid re-reading the data multiple
times, however I can't figure out how I could use the WRITE command to
direct the output to different files.
If there is someone out there with better insights into how I could
undertake this I would appreciate it.
Regards
Frank Milthorpe ...
---
Frank Milthorpe, Transport Modelling Manager
Transport Data Centre, NSW Department of Transport
GPO Box 1620, Sydney NSW 2001, AUSTRALIA
Ph: +61 2 9268 2937
Fax: +61 2 9268 2853
Email: frank.milthorpe@transport.nsw.gov.au