Create new variables, using old variable name plus _8

classic Classic list List threaded Threaded
14 messages Options
Reply | Threaded
Open this post in threaded view
|

Create new variables, using old variable name plus _8

ANDRES ALBERTO BURGA LEON

Hello to everybody on the list:

I have a data fiel, with string variables (60 or so, of diferent length), with names like:

PS465Q01
PS465Q02
PS465Q04
PS131Q02
PS131Q04
PS428Q01

I need to create new variables wit format F1.0, and new names like:

PS465Q01_8
PS465Q02_8
PS465Q04_8
PS131Q02_8
PS131Q04_8
PS428Q01_8

I need to repeat thyis operation with 13 diferent data bases, and I've done it with Excel: copiying the original variable names, and then ading a '_8' column (It would also wotk if I add a 'o_' at the begining of the variable name) containig both colums and pasting the result in SPSS.

I wonder if there a more eficient way (using SPSS syntax, I don't know anything about Phyton) to do this. I need to keep the original variables order.

Thank you very much

Andres




Reply | Threaded
Open this post in threaded view
|

Re: Create new variables, using old variable name plus _8

Ruben Geert van den Berg
Dear Andres,

If you "don't know anything about Python", then this is a terrific opportunity to discover it! After installing the Python essentials, you should be able to copy-paste-run the code below after editing just two lines containing a # sign.

begin program.
variables = 'v5 to v10' # Please specify the variables to be suffixed here.
suffix ='_2012' # Please specify the desired suffix here.
import spss,spssaux
oldnames = spssaux.VariableDict().expand(variables)
newnames = [varnam + suffix for varnam in oldnames]
spss.Submit("ren var (%s=%s)."%(" ".join(oldnames)," ".join(newnames)))
end program.

The Python essentials are a single installation file for recent versions and can be found here: https://www.ibm.com/developerworks/mydeveloperworks/wikis/home/wiki/We70df3195ec8_4f95_9773_42e448fa9029/page/Downloads%20for%20IBM%C2%AE%20SPSS%C2%AE%20Statistics?lang=en

If you like, I can adapt this code so it will run over your 13 (or 100 or 1000) datafiles in one go.

Kind regards,

Ruben


Date: Wed, 7 Nov 2012 12:00:01 -0500
From: [hidden email]
Subject: Create new variables, using old variable name plus _8
To: [hidden email]


Hello to everybody on the list:

I have a data fiel, with string variables (60 or so, of diferent length), with names like:

PS465Q01
PS465Q02
PS465Q04
PS131Q02
PS131Q04
PS428Q01

I need to create new variables wit format F1.0, and new names like:

PS465Q01_8
PS465Q02_8
PS465Q04_8
PS131Q02_8
PS131Q04_8
PS428Q01_8

I need to repeat thyis operation with 13 diferent data bases, and I've done it with Excel: copiying the original variable names, and then ading a '_8' column (It would also wotk if I add a 'o_' at the begining of the variable name) containig both colums and pasting the result in SPSS.

I wonder if there a more eficient way (using SPSS syntax, I don't know anything about Phyton) to do this. I need to keep the original variables order.

Thank you very much

Andres




Reply | Threaded
Open this post in threaded view
|

Automatic reply: Create new variables, using old variable name plus _8

Weinberg, Jerry

I will be away from my office from 11/8 through 11/9.

Reply | Threaded
Open this post in threaded view
|

Re: Create new variables, using old variable name plus _8

John F Hall
In reply to this post by ANDRES ALBERTO BURGA LEON

do repeat

               x= PS465Q01 PS465Q02 PS465Q04 PS131Q02 PS131Q04 PS428Q01

               y= PS465Q01_8 PS465Q02_8 PS465Q04_8 PS131Q02_8 PS131Q04_8 PS428Q01_8.

Compute y = x.

Format x (f1.0).

End repeat.

[Sorry about the spacing and the initial capitals: machine gone haywire]

 

John F Hall (Mr)

 

Email:     [hidden email]

Website: www.surveyresearch.weebly.com

 

 

 

 

From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of ANDRES ALBERTO BURGA LEON
Sent: 07 November 2012 18:00
To: [hidden email]
Subject: Create new variables, using old variable name plus _8

 


Hello to everybody on the list:

I have a data fiel, with string variables (60 or so, of diferent length), with names like:
PS465Q01
PS465Q02
PS465Q04
PS131Q02
PS131Q04
PS428Q01

 

 

I need to create new variables wit format F1.0, and new names like:

PS465Q01_8
PS465Q02_8
PS465Q04_8
PS131Q02_8
PS131Q04_8
PS428Q01_8

I need to repeat thyis operation with 13 diferent data bases, and I've done it with Excel: copiying the original variable names, and then ading a '_8' column (It would also wotk if I add a 'o_' at the begining of the variable name) containig both colums and pasting the result in SPSS.

I wonder if there a more eficient way (using SPSS syntax, I don't know anything about Phyton) to do this. I need to keep the original variables order.

Thank you very much

Andres



Reply | Threaded
Open this post in threaded view
|

Re: Create new variables, using old variable name plus _8

David Marso
Administrator
In reply to this post by ANDRES ALBERTO BURGA LEON
Look ma no python!
--
Sub Main
        Dim varlist () As String, cmd As String
        varlist=objSpssApp.Documents.GetDataDoc(0).GetVariables (False)
        cmd="NUMERIC "
        For i=0 To UBound(varlist)-1
                cmd=cmd & varlist(i) & "_8 "
        Next
        cmd=cmd & " (F1.0) ."
        objSpssApp.ExecuteCommands cmd,False
End Sub

ANDRES ALBERTO BURGA LEON wrote
Hello to everybody on the list:

I have a data fiel, with string variables (60 or so, of diferent length),
with names like:

PS465Q01
PS465Q02
PS465Q04
PS131Q02
PS131Q04
PS428Q01

I need to create new variables wit format F1.0, and new names like:

PS465Q01_8
PS465Q02_8
PS465Q04_8
PS131Q02_8
PS131Q04_8
PS428Q01_8

I need to repeat thyis operation with 13 diferent data bases, and I've
done it with Excel: copiying the original variable names, and then ading a
'_8' column (It would also wotk if I add a 'o_' at the begining of the
variable name) containig both colums and pasting the result in SPSS.

I wonder if there a more eficient way (using SPSS syntax, I don't know
anything about Phyton) to do this. I need to keep the original variables
order.

Thank you very much

Andres
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Reply | Threaded
Open this post in threaded view
|

Re: Create new variables, using old variable name plus _8

DKUKEC
Greetings David,

I have tried to use the syntax you posted in this string, however, it is not working with my dataset which  inlcudes 21 numeric variables.

Sub Main
        Dim varlist () As String, cmd As String
        varlist=objSpssApp.Documents.GetDataDoc(0).GetVariables (False)
        cmd="NUMERIC "
        For i=0 To UBound(varlist)-1
                cmd=cmd & varlist(i) & "_8 "
        Next
        cmd=cmd & " (F1.0) ."
        objSpssApp.ExecuteCommands cmd,False
End Sub.
 
Main

Where am I going wrong?

Thank you.
Damir
Reply | Threaded
Open this post in threaded view
|

Re: Create new variables, using old variable name plus _8

David Marso
Administrator
Define "isn't working"!!!
I read minds but it costs extra!
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Reply | Threaded
Open this post in threaded view
|

Re: Create new variables, using old variable name plus _8

DKUKEC
David,

From what I've see you do on this forum, I beg to differ :-).  Anyways, the syntax seems to work in that there are no error or warning messages;  however, the syntax does not create the variables.  The only message I see in the syntax output window is the word MAIN.

I must be missing something.

Cheers,
Damir
Reply | Threaded
Open this post in threaded view
|

Re: Create new variables, using old variable name plus _8

David Marso
Administrator
This need to be in a Script saved as an external file.
Invoke it from Syntax using the Script command (see FM for details).

DKUKEC wrote
David,

From what I've see you do on this forum, I beg to differ :-).  Anyways, the syntax seems to work in that there are no error or warning messages;  however, the syntax does not create the variables.  The only message I see in the syntax output window is the word MAIN.

I must be missing something.

Cheers,
Damir
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Reply | Threaded
Open this post in threaded view
|

Re: Create new variables, using old variable name plus _8

David Marso
Administrator
In addition, don't explicitly call Main.  It will invoke Main automatically.
Nature of the beast!

David Marso wrote
This need to be in a Script saved as an external file.
Invoke it from Syntax using the Script command (see FM for details).

DKUKEC wrote
David,

From what I've see you do on this forum, I beg to differ :-).  Anyways, the syntax seems to work in that there are no error or warning messages;  however, the syntax does not create the variables.  The only message I see in the syntax output window is the word MAIN.

I must be missing something.

Cheers,
Damir
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Reply | Threaded
Open this post in threaded view
|

Re: Create new variables, using old variable name plus _8

DKUKEC
After reviewing FM, Nabble, Google... no luck.  Any further assistance would be much appreciated.  Not sure where I am going wrong.

SCRIPT  FILE = 'G:\DATA\Research & Planning\CHARTS - MOVING\US UCR Summary Data\VARCHG.SPS' .

Error      : Unexpected text.
At Line No : 10

EXECUTE .

Cheers,
Damir
Reply | Threaded
Open this post in threaded view
|

Re: Create new variables, using old variable name plus _8

David Marso
Administrator
Well first of all SPS is *NOT* the extension used for Script files.
the period (.) is *NOT* appropriate in the End Sub line!
HTH

DKUKEC wrote
After reviewing FM, Nabble, Google... no luck.  Any further assistance would be much appreciated.  Not sure where I am going wrong.

SCRIPT  FILE = 'G:\DATA\Research & Planning\CHARTS - MOVING\US UCR Summary Data\VARCHG.SPS' .

Error      : Unexpected text.
At Line No : 10

EXECUTE .

Cheers,
Damir
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Reply | Threaded
Open this post in threaded view
|

Re: Create new variables, using old variable name plus _8

David Marso
Administrator
*ALSO* if you are going to use Basic Scripting you MAY want to CYA and use Option Explicit as first line of code.  Note you have *NOT* declared the loop variable (i), so please do that.

David Marso wrote
Well first of all SPS is *NOT* the extension used for Script files.
the period (.) is *NOT* appropriate in the End Sub line!
HTH

DKUKEC wrote
After reviewing FM, Nabble, Google... no luck.  Any further assistance would be much appreciated.  Not sure where I am going wrong.

SCRIPT  FILE = 'G:\DATA\Research & Planning\CHARTS - MOVING\US UCR Summary Data\VARCHG.SPS' .

Error      : Unexpected text.
At Line No : 10

EXECUTE .

Cheers,
Damir
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Reply | Threaded
Open this post in threaded view
|

Re: Create new variables, using old variable name plus _8

Richard Ristow
In reply to this post by ANDRES ALBERTO BURGA LEON
Back on 07 November 2012 18:00, ANDRES ALBERTO BURGA LEON wrote,

>I have a data file, with string variables (60 or so, of different
>length), with names like:
>PS465Q01   PS465Q02   PS465Q04   PS131Q02   PS131Q04   PS428Q01
>
>I need to create new variables with format F1.0, and new names like:
>PS465Q01_8 PS465Q02_8 PS465Q04_8 PS131Q02_8 PS131Q04_8 PS428Q01_8

|-----------------------------|---------------------------|
|Output Created               |13-FEB-2014 22:55:06       |
|-----------------------------|---------------------------|
  [TestData]
CASEID PS465Q01 PS465Q02 PS465Q04 PS131Q02 PS131Q04 PS428Q01

  1001  3        5        A        6        4        1
  1002  B        7        9        11       2        1

Number of cases read:  2    Number of cases listed:  2


DATASET COPY     TestCode.
DATASET ACTIVATE TestCode WINDOW=FRONT.

STRING  SUFFIX  (A1).
COMPUTE SUFFIX = '8'.

CASESTOVARS
   /ID=CASEID
   /AUTOFIX=NO
   /SEPARATOR="_"
   /INDEX = SUFFIX.

Cases to Variables
|-----------------------------|---------------------------|
|Output Created               |13-FEB-2014 22:55:07       |
|-----------------------------|---------------------------|
  [TestCode]
Warnings [for all variables, suppressed]
Generated Variables
|--------|------|----------|
|Original|SUFFIX|Result    |
|Variable|      |----------|
|        |      |Name      |
|--------|------|----------|
|PS465Q01|8     |PS465Q01_8|
|PS465Q02|8     |PS465Q02_8|
|PS465Q04|8     |PS465Q04_8|
|PS131Q02|8     |PS131Q02_8|
|PS131Q04|8     |PS131Q04_8|
|PS428Q01|8     |PS428Q01_8|
|--------|------|----------|

Processing Statistics
|---------------|---|
|Cases In       |2  |
|Cases Out      |2  |
|---------------|---|
|Cases In/Cases |1.0|
|Out            |   |
|---------------|---|
|Variables In   |8  |
|Variables Out  |7  |
|Index Values   |1  |
|---------------|---|

LIST.
List
|-----------------------------|---------------------------|
|Output Created               |13-FEB-2014 22:55:07       |
|-----------------------------|---------------------------|
  [TestCode]

CASEID PS465Q01_8 PS465Q02_8 PS465Q04_8 PS131Q02_8 PS131Q04_8 PS428Q01_8

  1001  3          5          A          6          4          1
  1002  B          7          9          11         2          1

Number of cases read:  2    Number of cases listed:  2


Now ALTER TYPE on the new variables and, if desired, MATCH FILES to
add them to the original file.
=============================
APPENDIX: Test data, and code
(not saved separately)
=============================
DATA LIST LIST
    /CASEID PS465Q01 PS465Q02 PS465Q04 PS131Q02 PS131Q04 PS428Q01
     (F4,    A5,      A4,       A6,     A6,       A4,      A4).
BEGIN DATA
      1001   3        5          A       6        4       1
      1002   B        7          9      11        2       1
END DATA.
DATASET NAME     TestData WINDOW=FRONT.
LIST.

DATASET COPY     TestCode.
DATASET ACTIVATE TestCode WINDOW=FRONT.

STRING  SUFFIX  (A1).
COMPUTE SUFFIX = '8'.

PRESERVE.
SET MAXWARNS 0.
CASESTOVARS
   /ID=CASEID
   /AUTOFIX=NO
   /SEPARATOR="_"
   /INDEX = SUFFIX.
RESTORE.
LIST.

=====================
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