Macro to change format of many variables

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

0101
Posts: 52
Joined: Fri Mar 04, 2011 9:42 pm

Macro to change format of many variables

Postby 0101 » Mon Feb 03, 2014 2:53 pm

I'm using a macro from Raynald's site to convert a list of variables from string to numeric:

http://www.spsstools.net/MacroTutorial.htm#Version3

But I noticed, even using the sample data list he provides, the macro ends up *deleting* the variables rather than converting them.

His first macro defines a macro named !list1 which creates a list of all variables (that you specify between var1 and var2) in a file you specify when you call the macro. This list is then used as one of the parameter in the second macro, conver2, which does the conversion.

Code: Select all

*///////////////////////////////////////.
DEFINE !DefList (var1=!TOKENS(1) 
		/var2=!TOKENS(1) 
		/fname=!CMDEND)

GET FILE=!fname.
COMPUTE dummy=1.
N OF CASES 1.
MATCH FILES FILE=* /BY dummy /KEEP=!var1 TO !var2.
FLIP.
COMPUTE dummy=1.

MATCH FILES FILE=* /BY dummy /FIRST=first /LAST=last.
DO IF first.
WRITE OUTFILE='c:\temp\list1.sps' / 'DEFINE !list1()' case_lbl.
WRITE OUTFILE='c:\temp\qlist1.sps' / 'DEFINE !qlist1()"' case_lbl'"'.
ELSE if not last.
WRITE OUTFILE='c:\temp\list1.sps' / " "case_lbl.
WRITE OUTFILE='c:\temp\qlist1.sps' / " '"case_lbl"'".
ELSE.
WRITE OUTFILE='c:\temp\list1.sps' /" "case_lbl ' !ENDDEFINE'.
WRITE OUTFILE='c:\temp\qlist1.sps' /" '"case_lbl"'" ' !ENDDEFINE'.
END IF.
EXECUTE.

GET FILE=!fname.
INCLUDE FILE='c:\temp\list1.sps'.
* qlist1 = list1 with quoted variable names.
INCLUDE FILE='c:\temp\qlist1.sps'.
!ENDDEFINE.
*///////////////////////////////////////.
The second macro converts the variables from string to numeric (or numeric to string depending on your preference).

Code: Select all

SET MPRINT=yes.
*/////////////////////.
DEFINE !conver2 (type=!TOKENS(1)
    /cformat=!TOKENS(1)
    /nformat=!TOKENS(1)
    /vnames=!CMDEND)

/* where.
* type     = type of change ss (string to string) ns or sn where n=numeric and s=string.
* cformat = the conversion format (only needed when type=ns).
* nformat = the new format.
* vnames= the names of the variables to change format.

!DO !vname !IN (!vnames)
!IF (!type='ss') !THEN STRING temp1234(!nformat).
COMPUTE temp1234=!vname.
!IFEND

!IF (!type='ns') !THEN
STRING temp1234(!nformat).
COMPUTE temp1234=LTRIM(STRING(!vname,!cformat)).
!IFEND                        

!IF (!type='sn') !THEN
COMPUTE temp1234=NUMBER(!vname,!nformat).
FORMAT temp1234(!nformat).
!IFEND                        

MATCH FILES FILE=* /DROP=!vname.
RENAME VARIABLE (temp1234=!vname).
!DOEND
!ENDDEFINE.
*/////////////////////.


Now test the macros. In this example, I'm left with a file containing only the last variable (vary).

Code: Select all

DATA LIST LIST /var1(A8) vnum2(A8) varx(A8) vary(A8).

BEGIN DATA
1235.23		254.13	235.00	6532.20 
53261.32	128.09	265.85	2591.99
END DATA.
LIST.

!DEfList var1=var1 var2=vary fname="c:/temp/mydata.sav". 
!conver2 type=sn cformat=F8.1 nformat=A8 vnames=!list1.


The syntax is attached.

Any ideas? The problem seems to happen in the last section of the conversion macro (the MATCH FILES FILE = * /DROP=!vname.) The variables are getting dropped but not restored.
You do not have the required permissions to view the files attached to this post.

Who is online

Users browsing this forum: No registered users and 2 guests

cron