LISTSERV at the University of Georgia
Menubar Imagemap
Home Browse Manage Request Manuals Register
Previous messageNext messagePrevious in topicNext in topicPrevious by same authorNext by same authorPrevious page (July 2005)Back to main SPSSX-L pageJoin or leave SPSSX-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:   Mon, 18 Jul 2005 09:35:12 -0500
Reply-To:   John Norton <>
Sender:   "SPSSX(r) Discussion" <SPSSX-L@LISTSERV.UGA.EDU>
From:   John Norton <>
Subject:   Re: formatting variables
Comments:   To: wajdowdc@YAHOO.COM
Content-Type:   text/plain; charset=US-ASCII


As Vishal pointed out, this was point of discussion from last week, so luckily the solutions are still fresh.

The solution he re-posted from me however is a cumbersome one in that it requires the user to re-execute the commands repetitively, once for each attempt to rename. So, I composed the a macro which will allow you to list all the string variables you want to reformat in one macro call. However, even that solution could have been a bit more efficient, according to a colleague, but I haven't had the chance to review his untested suggestion.. yet (Sorry, Neila.. I *just* got that post).

Because our conversations were carried on off list, I will re-post my macro below; my apologies if it's a bit long winded. Finally, I would recommend that you see the solution offered by Richard Ristow, as it is also a well explained and an efficient way to approach the problem.

Following is the macro (only slightly edited from their original) which I offered off list:


This macro needs only to be executed (or loaded into memory) once within the SPSS session within which you need to change the string lengths of the variables in your files. In other words, you'll load up SPSS, execute the macro definition - not the macro "call" - and then begin the process of opening your Excel files into SPSS, and then calling the macro to redefine the string lengths of the variables.

This "solution" is somewhat inelegant in as much as it will pass the data set once for each variable listed on the macro call. So, if you have 50 variables, that's 50 data passes through the processor, and there are some of my colleagues who might wince at that. [which it did, and his solution follows mine] Still, it gets the job done. I'll try to explain the code below in general terms, and then offer a "bottom line" for it's use.

- Bear in mind that the macro definition needs to be loaded into memory only once within the SPSS session. If you load it (execute the definition commands) twice, you'll get a warning stating that it's already been defined, but the most recent definition will take precedent.

- Macro language uses the "!" symbol a lot. It specifies the commands as reserved for the macro facility. Macros definitions are initialized and terminated with the commands DEFINE and !ENDDEFINE respectively.

Following is the macro. Copy it into a syntax window and save it with a meaningful name. Then, execute the commands by highlighting from the DEFINE statement through the !ENDDEFINE command, and then clicking on the Run [ > ] button at the top of the syntax editor.

* Macro definition. DEFINE restring (oldlist = !CHAREND("/") / size = !TOKENS(1)). !DO !I !IN (!oldlist). !CONCAT('STRING ',!I,'1 (A',!size,'). '). !CONCAT('COMPUTE ',!I,'1 = ',!I,'. '). MATCH FILES FILE = * /DROP = !I. EXE. !CONCAT('RENAME VARIABLES (',!I,'1 = ',!I,').'). !DOEND. !ENDDEFINE.

- I use several macro functions which I'll try to explain below: + The !CHAREND() function just tells the definition to reference the list of variables listed in the macro call, until it reaches a specified character which essentially tells SPSS, OK the list of variables stops here. + The !TOKENS() function allows me to specify a limit to the number of values I can specify for the macro argument here called "size". As we only need one value, I only allow 1 token. + The !CONCAT() function operates like the CONCAT() function in standard SPSS command language. Essentially, I use it to construct the commands I need, where the commands will embed the variables listed on the macro call.

- I use the command "!DO !I !IN (!oldlist)" to execute a loop, such that the process will execute once for each variable in the macro call, iterating once for each variable listed in the call, and which define "oldlist" (and occur before the "character end" (thus the function name "CHAREND" explained above) symbol.. here defined as "/". Loops require a command to terminate the loop, so I use the !DOEND command to terminate the loop.

Now, open an Excel file which has the problem string variables into SPSS. (likewise, you can execute this macro against any SPSS file, as once any file is opened and appears in the SPSS Data Editor, it's essentially an SPSS formatted file; all you have to do to save it is SPSS format is to, well, *save* it.) As the macro definition is already resident in SPSS's memory, all you have to do at this point is execute the macro call below, replacing the list of variables in my example with those variable names which you want to set to a specific string width. To set a specific string width for the variables listed on the call, I use "50" but you might want to set it to something different. Finally, notice again the character "/" which separates the list of variables for the first argument (here called "oldlist") from the value for the second argument (here called "size" and set to 50).

* Macro call. restring oldlist = strvar1 strvar2 strvar3 strvar4 strvar15 strvar10 /size = 50.

Bottom Line: Copy the macro definition into a syntax window (or open it anew after saving it), and execute it so as to load it into memory. Then open your data file into the SPSS syntax editor. In either the same syntax window or in a new one, copy the macro call and replace your string variable names in the list where my sample string variable names are. Also, specify the size you want your string variables to be, if you want them a size other than 50 characters long. Finally run the macro call and review the damage.. I mean differences. Then you can save the changes to the file in preparation for the merges.


I hope this long winded explanation helps.


>>> Dana <wajdowdc@YAHOO.COM> 7/17/2005 9:48 PM >>>

Is there a way to change the width of a string variable using syntax?

The problem is I am opening Excel files in SPSS and then trying to merge them, but when I open the Excel files in SPSS the same variables in different files are assigned different string widths. So I can't merge them.

Thanks, Dana

Back to: Top of message | Previous page | Main SPSSX-L page