|
Hello,
I a new at this so be patient please :) I need to rename all the variables in a couple of datasets by adding a short suffix to them. I was told in the comp.soft-sys.stat.spss newsgroup that I could do it with a macro, but I am too new at this to do it by myself. Bruce Weaver proposed an elegant solution of stacking datasets, but I am afraid this would make things mode difficult for me later, as I have to share this data with people who know as little as me about SPSS. I paste our discussion below for reference. From what I have read on R. Levesque's SPSS website, macros are really powerful ways of manipulating data without writing novel-long syntaxes. Still, I haven't found a way to do this. Could anyone offer me a scratch in the back ? Thanks ! Lorenzo QUOTE: ===================================== Lorenzo Sandini wrote: > Hello, > > Can anyone tell me how to rename all variables in a set, for example > appending a suffix at the end of every variable ? > > The same survey has been repeatedly filled by participants at regular > intervals, and the answers to the repeatd questions have been saved > into various tables of an Access database, with an identical name > (question1, question2, question3, ...) > > I imported the various tables as separate datasets (questionnaire 3mo, > 6mo, 9mo, etc...) and they all have the same variables, that I would > like to join into one dataset with variables like : > > question1_3mo, question1_6mo, question1_9mo, > question2_3mo, question2_6mo, question2_9mo, etc... > > This could be done by hand since the number of variable is reasonable, > but I'd be happy to learn something new and apply it later in bigger > datasets. > > Thanks for your help ! > > Lorenzo No doubt you could write a macro to do this. But it might be simpler to use ADD FILES to stack your individual data sets, then CASESTOVARS to restructure from the long to wide format. When stacking the files, use the /IN subcommand to create flags for the different time points, and then after merging all the files, compute an index variable to be used by CASESTOVARS. I don't have SPSS on this machine, so cannot generate an example, but I think something along these lines should work. ADD FILES file = "C:\MyFolder\data3mo.sav" / in = flag3mo / file = "C:\MyFolder\data6mo.sav" / in = flag6mo / file = "C:\MyFolder\data9mo.sav" / in = flag9mo . EXE. do if flag3mo. - compute time = 3 . else if flag6mo - compute time = 6 . else. - compute time = 9 . end if. exe. sort cases by id time. CASESTOVARS id=idvar / index = time / drop {list of unneeded variables }. -- Bruce Weaver [hidden email] www.angelfire.com/wv/bwhomedir "When all else fails, RTFM." =============================== ===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD |
|
At 01:02 PM 8/7/2008, Lorenzo Sandini wrote:
>I need to rename all the variables in a couple of datasets by >adding a short suffix to them. You've been given solutions using the heavy stuff: macros, scripts, Python. But you originally wrote, >Bruce Weaver proposed an elegant solution of stacking datasets [and >using CASESTOVARS] That raises the question: Why do you want to rename the variables? I'd guessed that you want to merge the different years into 'wide' form, with data for all surveys in a single record; and I was about to post essentially the CASESTOVARS solution that Bruce suggested. But, you wrote >I am afraid [Bruce Weaver's solution] would make things mode >difficult for me later, as I have to share this data with >people who know as little as me about SPSS. Here's where understanding your problem better, could help. I'd have thought that the solutions posted would be much harder for SPSS beginners to understand, than CASESTOVARS is. You want to rename the variables to solve some other problem. Maybe there's a more elegant solution to the overall problem. -Best of luck, Richard ===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD |
|
In reply to this post by Lorenzo Sandini
A text-editor based solution:
There has been a discussion concerning renaming all variables in a dataset. And the solutions have been elegant macros or python scripts. These are of course the best option if one needs to do the same procedure repeatedly on a file that changes from time to time. However, my worksituation, where nobody else would be able to read the macros or python codes, does suggest that sometimes a less elegant low-tech solution is called for. I would run the following line from the SPSS syntax editor. DISPLAY NAMES. Which gives an output like this: Var1 Var2 Var3 I would then cut and paste this list into my editor in a new empty file (I use UltraEdit32, but all proper editors intended for writing syntax will do). I put my editor in column mode, and mark all lines. This allows me to write the same text simultaneously on every line. Thus it does not really matter if I am writing one, two or thousand lines of syntax simultaneously. Then I just type in the beginning of the lines "RENAME VARIABLES (" Then I jump to the end of lines and type "=" Then I paste the variable names again in one long column Then I jump to the end of lines and type "_suffix )." Then I do a search and replace to remove all extra spaces before "_suffix", if the variable names are of varying length. In the end I will have a syntax that looks like this: RENAME VARIABLES (Var1 = Var1_suffix). RENAME VARIABLES (Var2 = Var2_suffix). RENAME VARIABLES (Var3 = Var3_suffix). I would then copy and paste this back to the SPSS syntax editor, where I run and save it for documentation. The advantages of the editor based solution, as I see it, are 1) it is a fast solution, because it is easy to remember and quick to implement. Renaming 100 variables takes perhaps 10 more seconds than just one variable. (In lorenzos example, after writing the syntax for the first set of RENAMES, all one needs to do is to do a search and replace for each suffix, to create new versions of the same transformations.). 2) it is easy to select and control which variables are renamed. 3) it requires only minimum level of skill. the rest of the staff will be able to figure out what has happened, even if I am not there. 4) having variable names present in transformations, makes it possible to track changes to a variable across many syntax files. Any professional quality texteditor will allow for a search in many *.sps files for "variablename" and present the results as a list lines containing "variablename". This makes it easy to figure out what happens to a piece of information across several transformations. This makes it also possible to implement changes of variable names across all my syntax files at once, to ensure that they are all consistent. 5) better portability of the syntax. This can be a problem if somebody else is using the syntax or if I myself want to use the same syntax after a few years on a different computer using different version of SPSS and python. 6) writing many lines at once keeps the typoes down to a minimum, and if they are there, they are easy to find, as they effect every line of the code. The downside: 1) If a variable is added into the orginal file, I need to add one line of code to the RENAME VARIABLES, too. 2) it is easy to forget to do 1). This is the best reason for using a macro or a python solution, in my view. 3) It creates lots of code (which sometimes is an advantage as it usually easy to read). Best, Eero Olli ________________________________________ Eero Olli Advisor the Equality and Anti-discrimination Ombud [hidden email] +47 2405 5951 POB 8048 Dep, N-0031 Oslo, Norway ===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD |
|
In Excel, if the first varname is in cell A1, you could also use the following formula:
="rename variables ("&A1&"="&A1"_suffix)." The ampersand concatenates the strings. Cheers!! Albert-Jan --- On Thu, 8/14/08, Eero Olli <[hidden email]> wrote: > From: Eero Olli <[hidden email]> > Subject: Re: rename all variables in a dataset > To: [hidden email] > Date: Thursday, August 14, 2008, 3:58 PM > A text-editor based solution: > > There has been a discussion concerning renaming all > variables in a > dataset. And the solutions have been elegant macros or > python scripts. > These are of course the best option if one needs to do the > same > procedure repeatedly on a file that changes from time to > time. > > However, my worksituation, where nobody else would be able > to read the > macros or python codes, does suggest that sometimes a less > elegant > low-tech solution is called for. > > I would run the following line from the SPSS syntax editor. > > DISPLAY NAMES. > > Which gives an output like this: > > Var1 > Var2 > Var3 > > I would then cut and paste this list into my editor in a > new empty file > (I use UltraEdit32, but all proper editors intended for > writing syntax > will do). > > I put my editor in column mode, and mark all lines. This > allows me to > write the same text simultaneously on every line. Thus it > does not > really matter if I am writing one, two or thousand lines of > syntax > simultaneously. > > Then I just type in the beginning of the lines "RENAME > VARIABLES (" > > Then I jump to the end of lines and type "=" > Then I paste the variable names again in one long column > Then I jump to the end of lines and type "_suffix > )." > Then I do a search and replace to remove all extra spaces > before > "_suffix", if the variable names are of varying > length. > > In the end I will have a syntax that looks like this: > > RENAME VARIABLES (Var1 = Var1_suffix). > RENAME VARIABLES (Var2 = Var2_suffix). > RENAME VARIABLES (Var3 = Var3_suffix). > > I would then copy and paste this back to the SPSS syntax > editor, where I > run and save it for documentation. > > The advantages of the editor based solution, as I see it, > are > 1) it is a fast solution, because it is easy to remember > and quick to > implement. Renaming 100 variables takes perhaps 10 more > seconds than > just one variable. (In lorenzos example, after writing the > syntax for > the first set of RENAMES, all one needs to do is to do a > search and > replace for each suffix, to create new versions of the same > transformations.). > 2) it is easy to select and control which variables are > renamed. > 3) it requires only minimum level of skill. the rest of the > staff will > be able to figure out what has happened, even if I am not > there. > 4) having variable names present in transformations, makes > it possible > to track changes to a variable across many syntax files. > Any > professional quality texteditor will allow for a search in > many *.sps > files for "variablename" and present the results > as a list lines > containing "variablename". This makes it easy to > figure out what > happens to a piece of information across several > transformations. This > makes it also possible to implement changes of variable > names across all > my syntax files at once, to ensure that they are all > consistent. > 5) better portability of the syntax. This can be a problem > if somebody > else is using the syntax or if I myself want to use the > same syntax > after a few years on a different computer using different > version of > SPSS and python. > 6) writing many lines at once keeps the typoes down to a > minimum, and if > they are there, they are easy to find, as they effect every > line of the > code. > > The downside: > 1) If a variable is added into the orginal file, I need to > add one line > of code to the RENAME VARIABLES, too. > 2) it is easy to forget to do 1). This is the best reason > for using a > macro or a python solution, in my view. > 3) It creates lots of code (which sometimes is an advantage > as it > usually easy to read). > > > > Best, > Eero Olli > > ________________________________________ > Eero Olli > Advisor > the Equality and Anti-discrimination Ombud > [hidden email] +47 2405 5951 > POB 8048 Dep, N-0031 Oslo, Norway > > ===================== > To manage your subscription to SPSSX-L, send a message to > [hidden email] (not to SPSSX-L), with no body > text except the > command. To leave the list, send the command > SIGNOFF SPSSX-L > For a list of commands to manage subscriptions, send the > command > INFO REFCARD ===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD |
| Free forum by Nabble | Edit this page |
