Create dummy variables
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | * This is a general macro to create dummy variables. * rlevesque@videotron.ca 2002/12/28. SET MPRINT=no. * Save the following macro in a file called dumcode.sps. *////////////////////. DEFINE !DumCode(NomVar =!CharEnd ('/') /PreNam =!CharEnd ('/') /RefCat =!DEFAULT("NONE") !CharEnd ('/') /FPath =!CMDEnd) /* NomVar Name of nominal variable for which dummy var need to be created */ /* PreNam Root (or stem) of new dummy variables */ /* RefCat No dummy variable is created for the Reference Category */ /* (Do not use this parameter if there is none) */ /* FPath Full path and name of data file */ GET FILE=!FPath. *Find all categories. AGGREGATE /OUTFILE=* /BREAK=!NomVar /N_BREAK=N. STRING vname1 TO vname3 (A7). COMPUTE catvalue=!NomVar. COMPUTE vname1=RTRIM(LTRIM(STRING(catvalue,F8.0))). COMPUTE vname2=!QUOTE(!NomVar). COMPUTE vname3=CONCAT(!QUOTE(!EVAL(!PreNam)),vname1). DO IF catvalue<>!RefCat OR !QUOTE(!RefCat)="NONE". WRITE OUTFILE='c:\\temp\\define dum.sps' / 'COMPUTE ' vname3 '=(' vname2 '=' catvalue ').' / 'VALUE LABELS ' vname3 ' 0 "no" 1 "yes".'. END IF. EXECUTE. GET FILE=!FPath. INCLUDE 'C:\\temp\\define dum.sps'. EXECUTE. !ENDDEFINE. *////////////////////. * One does not need to know the number of categories to call the macro. * the categories can equal 0, or any positive integer. * the categories do not have to be consecutive. * Using the INCLUDE statement, only 2 lines of code are needed to run the macro. INCLUDE 'c:\\temp\\dumcode.sps'. *Next line creates dummy variables when 0 is the reference variable. !DumCode NomVar=jobcat /PreNam=job /RefCat=1 /FPath='C:\\Program Files\\SPSS\\employee data.sav'. * Next line creates dummy variables when 2 is the reference variable. !DumCode NomVar=jobcat /PreNam=job /RefCat=2 /FPath='C:\\Program Files\\SPSS\\employee data.sav'. * Next line creates dummy variables when there is no reference variable. !DumCode NomVar=jobcat /PreNam=job /FPath='C:\\Program Files\\SPSS\\employee data.sav'. *****************************. * Treatment of missing or negative variable: * System missing or user missing values results in missing values of the dummy variables. * No tests are made that the values of NomVar are indeed integers. |
Related pages
...