Merge text data files

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

cintijohn
Posts: 6
Joined: Wed Jul 13, 2011 5:37 pm

Merge text data files

Postby cintijohn » Mon Jul 21, 2014 4:10 pm

I have to write out several data files (.txt) and I'm only capturing certain portions of the data. Is it possible that I can merge these individual text files into one (ADD Files errors out)? :?

Current code...

Code: Select all

get file='C\Test\2013_data.sav'.

compute resp_id=TransID.
format resp_id(n11).
execute.

recode q8832 q8833 q8834 (missing=0).
execute.

do if (q8832>0).
	write outfile='C\Test\test1.txt'
	/'!99999'resp_id'|8832|'q8832(f2.0)'~'
.
end if.
do if (q8833>0).
	write outfile='C\Test\test2.txt'
	/'!99999'resp_id'|8833|'q8833(f2.0)'~'
.
end if.
do if (q8834>0).
	write outfile='C\Test\test3.txt'
	/'!99999'resp_id'|8834|'q8834(f2.0)'~'
.
end if.
execute.

 ADD FILES
    /FILE="C\Test\test1.txt"
    /FILE="C\Test\test2.txt"
    /FILE="C\Test\test3.txt".
  EXECUTE.
SAVE OUTFILE="C\Test\test_master.txt".
RubenGeert
Posts: 100
Joined: Mon May 19, 2014 6:06 am

Re: Merge text data files

Postby RubenGeert » Mon Jul 21, 2014 7:19 pm

Do you only need the master file or the separate .txt files as well? In the latter case, try using DO IF ... ELSE IF ... ELSE IF ... (and so on until) END IF. and specify the same file name after each condition.

WRITE OUTFILE only "keeps in mind" the data it has to write. It will only write the actual file after a data pass (possibly triggered by EXECUTE).

I'm not sure what exactly you're trying to accomplish but if it takes a lot of work/many lines of syntax, see if you can automate it with Python. Btw, Python can also very easily merge separate text files for you. You don't even need to type in the file names for that :-D

Kind regards,

Ruben Geert van den Berg
www.spss-tutorials.com
cintijohn
Posts: 6
Joined: Wed Jul 13, 2011 5:37 pm

Re: Merge text data files

Postby cintijohn » Mon Jul 21, 2014 7:54 pm

I'm looking to create one "master" .txt file. I hadn't given python any thought but I'll definitely investigate merging through that program.

Thanks for your help :D !
RubenGeert
Posts: 100
Joined: Mon May 19, 2014 6:06 am

Re: Merge text data files

Postby RubenGeert » Mon Jul 21, 2014 8:01 pm

If you're not familiar with Python yet, look here: http://www.spss-tutorials.com/introduci ... t-and-why/

Finding all files with a given extension is done here: http://www.spss-tutorials.com/move-all- ... in-folder/

Let me know whether that gets you any further. If not, I can perhaps write the syntax for you tomorrow.

Best,

Ruben

Who is online

Users browsing this forum: No registered users and 1 guest

cron