*========================================================================. * * Explanation PreML.inc * * Last update: Friday November, 10, 2006. * Procedure written by Jurjen Iedema, Social Cultural Planning Office, * The Hague, The Netherlands ; translated by Tom Snijders, * University of Groningen, The Netherlands . * * This file is an SPSS include file (the filename must be PreML.inc). * Note that all lines starting with * are comment lines, intended for the * human reader. The lines not starting with * are read by SPSS. * * What is the effect of PreML.inc? * Within SPSS PreML.inc can be called. This makes available a procedure * PreML that can make a file with data AND variable names, for input * into MLWiN. * * PreML.inc does the following: * - it sorts the SPSS file according to one or more level identifiers; * - it calculates the constant (called 'cons'); * - and a respondent identifier (called 'resp'); * - data and variable names of SPSS are made available to MlwiN * by means of a macro; * - all cases with missing values can be deleted 'listwise'; * - decimal comma's (used in the Netherlands) can be converted to * decimal dots (necessary for MLWin). * * * Procedure within SPSS * Get a data file (e.g. get file='c:\data\b1.sav'). * See to it that the missings are defined correctly! * (i.e. system or user missing.) * * Give the two following commands (after changing variable & filenames): * The include command has to be executed only once during a Spss session. * INCLUDE 'c:\myfiles\PREML.INC'. * PREML * MLVAR = region group * /OVAR = varia varib var1 to var7 * /FILE = 'c:\tmp\PreML.dat' * /LIWIDEL = yes * /CONVCOM = yes. * * Everything in CAPITALS must be copied literally. * Give the correct path name (in this example, 'c:\myfles\PREML.INC') to * the directory where PREML.INC is stored. * The variable names and an output filename are given to procedure PreML. * The subcommands of PreML are: * MLVAR: one or more level identifiers (here 'region' and 'group'). The * data are sorted on these variables. With two or more level * identifiers then first give the higher level identifier (here * region) and subsequently the lower level identifiers (group). * These variables should have integer values. * OVAR : the other variables to be transported to MLwiN (here 'varia * varib var1 to var7'). Commas are allowed but not required. * The 'to' keyword is allowed once. A Common Error: Make sure a * variable is named only once, either as MLVAR or as OVAR. * FILE : the filename (here 'c:\tmp\PreML.dat') that you want to create * to be used as a macro within MLwiN for getting the data into * MLwiN. The default filename is 'PreML.dat' stored in the * default temporary directory (defined by %temp%). * LIWIDEL: this controls listwise deletion of missing values. The default * YES deletes every case with one or more missing values. * LIWIDEL = NO: does not delete cases, but recodes all missing * values to the default missing value of MLWin versions 1 thru * 2.02 (-9.999E+29: a very large negative value). * CONVCOM: Convert decimal comma's to decimal dots (e.g. 6,53 becomes * 6.53). Yes is the default, no does not convert comma's. * Because of the default values for LIWIDEL and CONVCOM, a call to PreML * can be as short as: * PREML MLVAR = region group /OVAR = varia varib var1 to var7 * /FILE = 'c:\tmp\PreML.dat' * * What kind of file is made? * The output file of PreML.inc (called PreML.dat in this example) * contains the data with some lines before and some lines after the data. * This additional information makes the file a macro which can be used * for MlwiN to give not only the data but also the variable names. * * * How to continue in MLwiN? * Start MLwiN, click Data Manipulation and then Command interface * (in the older version the Command Interface is under Window). * Click the bottom line and give the command: OBEY filename, * e.g.: OBEY c:\tmp\PreML.dat * To check whether this went successfully, click Data Manipulation * and then Names (or in the older version: Window; Names). * When it is ok, save the worksheet now as MLWin usually crashes when you * at once try to specify the Model Equations. * *========================================================================. define PreML (Mlvar=!charend('/') /Ovar=!charend('/') /File=!default('"%temp%\PreML.dat"') !charend('/') /Liwidel=!default(yes) !charend('/') /Convcom=!default(yes) !cmdend) preserve set results off printback=off mprint off comp totmis=0. /* Compute # missings per case. comp nvars=2. /* Compute # of variables (including cons & resp). do repeat varlist=!MLvar, !Ovar. if missing(varlist) totmis=totmis+1. comp nvars=nvars+1. end repeat. !if (!eval(!upcase(!Liwidel)=YES)) !then select if totmis=0. /* listwise deletion. !else recode !MLvar, !Ovar (missing=-9.999E+29). !ifend. sort cases by !MLvar. compute resp=$casenum. compute cons=1. * Add variable maxresp: indicates the number of the last case. match files /file=* /by cons /last=maxresp. *** Write info about the # of variables before the first case. vector hstr(5 a255). compute hstr1=''. do if $casenum=1. compute hstr1=rtrim(concat(rtrim(hstr1),'assign c1-c', ltrim(string(nvars,f6.0)))). write outfile= !file /'Echo 0' /hstr1. end if. * Write the data. compute strnum=1. compute hstr1=''. compute hstr2=''. compute hstr3=''. compute hstr4=''. compute hstr5=''. string hstr_h hstr_h2 hstr_h3 (a255). compute hstr_h=''. do repeat varlist=!MLvar, resp, cons, !Ovar. compute hstr_h= ltrim(rtrim(string(varlist,f12.5))). * Rounding of extremely small numbers. do if (varlist*10000 = rnd(varlist)*10000). compute hstr_h= ltrim(rtrim(string(varlist,f12))). else. * Remove any redundant zero at the end of a number. compute hstr_h= rtrim(rtrim(rtrim(rtrim(hstr_h),'0'),','),'.'). if length(rtrim(hstr_h))=0 hstr_h='0'. end if. * Replace comma with period. !if (!eval(!upcase(!Convcom)=YES)) !then comp hcomma=rindex(hstr_h,','). if hcomma=0 hcomma=length(rtrim(hstr_h))+1. comp hstr_h2=''. comp hstr_h3=''. comp hstr_h2=substr(hstr_h,1,hcomma-1). comp hstr_h3=substr(hstr_h,hcomma+1). do if length(rtrim(hstr_h3))>0. comp hstr_h=concat(rtrim(hstr_h2),".",rtrim(hstr_h3)). else. comp hstr_h=rtrim(hstr_h2). end if. !ifend. if (length(rtrim(hstr(strnum))) + length(rtrim(hstr_h)) >= length(hstr(strnum))) strnum=strnum+1. compute hstr(strnum)= concat(rtrim(hstr(strnum)), ' ', rtrim(hstr_h)). end repeat. do if strnum=1. write outfile= !file /hstr1. else if strnum=2. write outfile= !file /hstr1 to hstr2. else if strnum=3. write outfile= !file /hstr1 to hstr3. else if strnum=4. write outfile= !file /hstr1 to hstr4. else if strnum=5. write outfile= !file /hstr1 to hstr5. end if. * Add last lines. do if maxresp. write outfile= !file /'finish' /'echo 1'. end if. exe. set results on. missing values !MLvar, resp, cons, !Ovar (-9.999E+29). descrip !MLvar, resp, cons, !Ovar. do if $casenum=1. write /'=======================================================' /'|| See the above for statistics of the variables ||' /'|| as written to the MLWin-file. ||' /'|| This is the end of the SPSS PreML-Macro. ||' /'|| You can start your MLWin-session now! ||' /'======================================================='. end if. exe. set results off. *** Write variablenames as strings. select if ($casenum<=1). flip variables = !MLvar, resp, cons, !Ovar. * Write the variablenames to a temporary file and join files later. string hstr1 (a255). compute respno=$casenum. compute hstr1=''. compute hstr1= concat(rtrim(hstr1), 'name c', ltrim(string(respno,f5.0)), ' "',rtrim(case_lbl), '"'). save outfile= '%temp%\temp.tmp' /keep hstr1. *** Join the data file and the file with variablenames. data list file=!file /hstr1 to hstr5 (5a255). add files /file=* /file='%temp%\temp.tmp'. compute strnum=5. if length(rtrim(hstr5))=0 strnum=4. if length(rtrim(hstr4))=0 strnum=3. if length(rtrim(hstr3))=0 strnum=2. if length(rtrim(hstr2))=0 strnum=1. do if strnum=1. write outfile= !file /hstr1. else if strnum=2. write outfile= !file /hstr1 to hstr2. else if strnum=3. write outfile= !file /hstr1 to hstr3. else if strnum=4. write outfile= !file /hstr1 to hstr4. else if strnum=5. write outfile= !file /hstr1 to hstr5. end if. exe. data list free /VAR00001 (a8). begin data. ' ' end data. show messages. erase file='%temp%\temp.tmp'. /* remove temporary file. restore. !enddefine.