Here is a series of 3 posts to newsgroup comp.soft-sys.stat.spss concerning a macro to run a series of simple linear regressions. Thanks to Neila Nessa for demonstrating how one can cycle through lists of X and Y variables. Bruce Weaver bweaver@lakeheadu.ca 12/Oct/2004 ------------------------------------------------------------- -------------------- Post Number 1 -------------------------- ------------------------------------------------------------- Subject: Re: Macro to run several univariate regressions, saving coefficients to files From: Bruce Weaver Date: Wed, 06 Oct 2004 10:33:16 -0400 Newsgroups: comp.soft-sys.stat.spss Bruce Weaver wrote: --- snip a lot of pre-amble ----- This is a macro I posted a while ago (for running a series of simple linear regressions with variables X1, X2, X3, etc): > DEFINE !SLR (Y = !tokens(1) / > numvars = !tokens(1) / > path = !tokens(1) / > root = !tokens(1)). > !DO !i = 1 !to !numvars. > !LET !file = !quote(!concat(!unquote(!path),!unquote(!root),!i,'.sav')) > !LET !x = !concat('x',!i) > TITLE I = !i, FILE = !file. > REGRESSION > /STATISTICS = COEFF > /DEPENDENT !Y > /METHOD=ENTER !x > /OUTFILE=COVB(!file) . > !DOEND. > !ENDDEFINE. I just wanted to mention that the TITLE line in the macro was *very* useful for debugging purposes as I worked on it (e.g., it helped me work out how to construct the file name properly). But once I had the macro working, I removed that line, because it just creates unneeded and unwanted output at that point. Cheers, Bruce -- Bruce Weaver bweaver@lakeheadu.ca www.angelfire.com/wv/bwhomedir ------------------------------------------------------------- -------------------- Post Number 2 -------------------------- ------------------------------------------------------------- Subject: Re: Macro to run several univariate regressions, saving coefficients to files From: neilanessa@msn.com (Neila Nessa) Date: 7 Oct 2004 18:52:14 -0700 Newsgroups: comp.soft-sys.stat.spss Hi All, Here is a lean, clean,mean approach which takes very little code and minimal grey matter to comprehend ;-) Note the use of !HEAD !TAIL operations to traverse parallel lists. Ah also as a side note, consider !CONCAT along with !LENGTH as a way to do simple math ;-) Neila data list free / a b c d e f g h . begin data blah blah blah boring test data ...... end data ** NOT TO BE POSTED ON ANY WEBSITE WITHOUT EXPRESSED ** ** WRITTEN PERMISSION AND PROPER ATTRIBUTION/CREDIT ** **NN/DMM **. DEFINE !REG ( XLIST !ENCLOSE("(",")") /YLIST !ENCLOSE("(",")") / COMBFIL !TOKENS(1) / PATH !CMDEND ) **SETUP INITIALIZATION*. !LET !FNAMES = !NULL !LET !XCOPY=!XLIST **PARALLEL PROCESS VARIABLE LISTS ** !DO !Y !IN (!YLIST) !LET !X = !HEAD(!XCOPY) !LET !XCOPY=!TAIL(!XCOPY) ** BUILD FILE NAME !LET !FILE = !QUOTE(!CONCAT(!UNQUOTE(!PATH),!X,"_",!Y)) ** BUILD ADD FILES LIST **. !LET !FNAMES= !CONCAT(!FNAMES, "/FILE=",!FILE ) ** RUN REGRESSION AND SAVE FILE ** . REGRESSION / DEPENDENT !Y / METHOD=ENTER !X / OUTFILE=COVB(!FILE) . !DOEND ** ADD FILES COMMAND **. ADD FILES !FNAMES . ** SAVE COMBINED FILE **. SAVE OUTFILE !QUOTE(!CONCAT(!UNQUOTE(!PATH),!UNQUOTE(!COMBFIL))). ************************************. ** THIS IS A LEAVE NO TRACE EVENT **. ** BURN BABY BURN **. ************************************. !LET !XCOPY=!XLIST !DO !Y !IN (!YLIST) !LET !X = !HEAD(!XCOPY) !LET !XCOPY=!TAIL(!XCOPY) ** BLAST THE TEMP FILES ONE AT A TIME **. !LET !FILE = !QUOTE(!CONCAT(!UNQUOTE(!PATH),!X,"_",!Y)) ERASE FILE !FILE. !DOEND !ENDDEFINE SET MPRINT ON / PRINTBACK ON . !REG XLIST (a b c d) YLIST (e f g h) COMBFIL = "Combined.sav" PATH "C:\TEMP\" . ------------------------------------------------------------- -------------------- Post Number 3 -------------------------- ------------------------------------------------------------- Subject: Re: Macro to run several univariate regressions, saving coefficients to files From: Bruce Weaver Date: Fri, 08 Oct 2004 15:51:40 -0400 Newsgroups: comp.soft-sys.stat.spss Nice one, Neila. That looks more like what I wanted in the first place-- it gets rid of the need to rename the variables to X1, X2, etc. There are a few small flies in the ointment though. First, I get this error message right after the first !DOEND (after the REGRESSION line): >Error # 6861 in column 1. Text: !DOEND >There is no matching !DO command for an occurrence of the !DOEND command. >This command not executed. Apparently, the problem is related to either the comments, or the blank lines, because the following works fine: DEFINE !REG ( XLIST !ENCLOSE("(",")") /YLIST !ENCLOSE("(",")") / COMBFIL !TOKENS(1) / PATH !CMDEND ) . !LET !FNAMES = !NULL !LET !XCOPY=!XLIST !DO !Y !IN (!YLIST) !LET !X = !HEAD(!XCOPY) !LET !XCOPY=!TAIL(!XCOPY) !LET !FILE = !QUOTE(!CONCAT(!UNQUOTE(!PATH),!X,"_",!Y)) !LET !FNAMES= !CONCAT(!FNAMES, "/FILE=",!FILE ) REGRESSION / DEPENDENT !Y / METHOD=ENTER !X / OUTFILE=COVB(!FILE) . !DOEND ADD FILES !FNAMES . SAVE OUTFILE !QUOTE(!CONCAT(!UNQUOTE(!PATH),!UNQUOTE(!COMBFIL))). !LET !XCOPY=!XLIST !DO !Y !IN (!YLIST) !LET !X = !HEAD(!XCOPY) !LET !XCOPY=!TAIL(!XCOPY) !LET !FILE = !QUOTE(!CONCAT(!UNQUOTE(!PATH),!X,"_",!Y)) ERASE FILE !FILE. !DOEND !ENDDEFINE . The second small problem is that I intended this macro for the situtation where Y is the same variable each time, so I'd like to declare Y only once. I solved that problem by changing the Y-line back to !TOKENS(1) (see below). The third problem is that I want the Regression call and Add Files calls to be separate rather than combined in one macro. (I may want to run a series of regressions, then do some other analyses, etc, BEFORE going on to examine the coefficients.) I solved that problem too. So, here's what I've got now: * ---- Macro Definitions ----- . * Macro to run a series of simple linear regressions, all using the same Y-variable. DEFINE !SLR ( XLIST !ENCLOSE("(",")") / Y !TOKENS(1) / PATH !CMDEND ) . !LET !XCOPY=!XLIST !DO !X !IN (!XLIST) !LET !FILE = !QUOTE(!CONCAT(!UNQUOTE(!PATH),!X,"_",!Y,".sav")) REGRESSION / DEPENDENT !Y / METHOD=ENTER !X / OUTFILE=COVB(!FILE) . !DOEND !ENDDEFINE . * Macro that uses ADD FILES to stack all the files of regression coefficients. DEFINE !stack ( XLIST !ENCLOSE("(",")") / Y !TOKENS(1) / COMBFIL !TOKENS(1) / PATH !CMDEND ) . !LET !FNAMES = !NULL !LET !XCOPY=!XLIST !DO !X !IN (!XLIST) !LET !FILE = !QUOTE(!CONCAT(!UNQUOTE(!PATH),!X,"_",!Y,".sav")) !LET !FNAMES= !CONCAT(!FNAMES, "/FILE=",!FILE ) !DOEND ADD FILES !FNAMES . SAVE OUTFILE !QUOTE(!CONCAT(!UNQUOTE(!PATH),!UNQUOTE(!COMBFIL))). !LET !XCOPY=!XLIST !DO !X !IN (!XLIST) !LET !FILE = !QUOTE(!CONCAT(!UNQUOTE(!PATH),!X,"_",!Y,".sav")) ERASE FILE !FILE. !DOEND !ENDDEFINE . * ----- Calling the macros ------ . !SLR XLIST (x1 x2 x3) Y = myDV COMBFIL = "Combined.sav" PATH = "C:\TEMP\" . !stack XLIST (x1 x2 x3) Y = myDV COMBFIL = "Combined.sav" PATH = "C:\TEMP\" . * ----- End of syntax ------- . With your permission, Neila, I will post these to my website, including your example that has lists for both X and Y. Cheers, Bruce -- Bruce Weaver bweaver@lakeheadu.ca www.angelfire.com/wv/bwhomedir