Rearranging a large number of variables with a mix of numeric and non-numeric variables

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

Rearranging a large number of variables with a mix of numeric and non-numeric variables

Staffan Lindberg
Dear list!

I'm posing this question for a collegue of mine:

He has a file with the following set of variables:

1. BR (Crime=Numeric)
2. PN (Penalty=Numeric)
3. TI  (Timer=Time format)
4. CO (Comment=String format)

The file is arranged as follows

BR1, PN1, TI1, CO1, BR2, PN2, TI2, CO2,............................,BR2081,
PN2081, TI2081, CO2081.

Now he wants to do some transformations on the numeric variables, for
instance:

RECODE BR1, BR2, .........................................., BR2081
(Something).

Does he have to specify each BR-variable or is there a simpler way?
Obviously he can't use the TO-convention since the variables are not
consecutive. Rearranging them would also take an extrordinary amount of
time. Is this solvable?

best

Staffan Lindberg
Sweden

=====================
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
Reply | Threaded
Open this post in threaded view
|

SV: Rearranging a large number of variables with a mix of numeric and non-numeric variables

Wilhelm Svensson | Queue Sweden AB
Hi there Staffan!

I did a small Script (not sure if you have Python) for you, hope it will
solve your problem.

Just create a new Script file in SPSS. Empty it from the empty sub created
by default and copy and paste the following in.

'*************************************
Option Explicit
Sub Main

'*************************************
' Wilhelm Svensson | Queue Sweden AB | www.qsweden.se
'*************************************

Dim objSPSSInfo As ISpssInfo,ValueLabels()
Dim I, NumVars As Long
Dim VarName As String
Dim ss, cc, pp, tt, ll As String

Dim DataDoc As ISpssDataDoc
Set objSPSSInfo = objSpssApp.SpssInfo
Set DataDoc = objSpssApp.Documents.GetDataDoc(0)

NumVars=objSPSSInfo.NumVariables-1

ss = InputBox("Start of the variable you want to recode:")
ll = InputBox("How to be recoded:","Recode as following:" )
pp = InputBox("Prefix for new variables:")

If Len(ss) = 0 Then Exit Sub

tt = ""

For I=0 To NumVars
  VarName= objSPSSInfo.VariableAt(I)
  If Mid(LCase(VarName),1,Len(ss)) = LCase(ss) Then
        tt = tt & "recode " & VarName & " " & ll & " into " & pp & VarName &
"." & vbCrLf
  End If
Next I

Dim sy As ISpssSyntaxDoc
Set sy = objSpssApp.NewSyntaxDoc

sy.Text = tt
sy.Visible = True

Set DataDoc = Nothing
Set sy = Nothing
Set objSPSSInfo = Nothing


End Sub

'*************************************

Run = Press f5 inside this script.

All the best


Wilhelm Svensson | +46-735-460000 | [hidden email]
Queue Sweden AB - A Management Science Company | www.qsweden.com

-----Ursprungligt meddelande-----
Från: SPSSX(r) Discussion [mailto:[hidden email]] För Staffan
Lindberg
Skickat: den 8 mars 2008 15:07
Till: [hidden email]
Ämne: Rearranging a large number of variables with a mix of numeric and
non-numeric variables

Dear list!

I'm posing this question for a collegue of mine:

He has a file with the following set of variables:

1. BR (Crime=Numeric)
2. PN (Penalty=Numeric)
3. TI  (Timer=Time format)
4. CO (Comment=String format)

The file is arranged as follows

BR1, PN1, TI1, CO1, BR2, PN2, TI2, CO2,............................,BR2081,
PN2081, TI2081, CO2081.

Now he wants to do some transformations on the numeric variables, for
instance:

RECODE BR1, BR2, .........................................., BR2081
(Something).

Does he have to specify each BR-variable or is there a simpler way?
Obviously he can't use the TO-convention since the variables are not
consecutive. Rearranging them would also take an extrordinary amount of
time. Is this solvable?

best

Staffan Lindberg
Sweden

=====================
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
Reply | Threaded
Open this post in threaded view
|

Re: Rearranging a large number of variables with a mix of numeric and non-numeric variables

Peck, Jon
In reply to this post by Staffan Lindberg
With SPSS 16, you can sort the variables in a dataset according to their names or any of their attributes, including type.

With any version since SPSS 14, programmability can be used to select sets of variables based on name wildcards or to reorder the dataset variables.

Here is an example of sorting by name (the sort is case sensitive, which could be changed if needed.)  Then TO could be used in the recode.

begin program.
import spss, spssaux
vard = spssaux.VariableDict()
alpha = sorted(vard.variables)
spss.Submit("ADD FILES FILE=* /KEEP=%s" % " ".join(alpha))
end program.

Here is an example that defines a macro listing all the numeric variables that could be used in RECODE, for example.

begin program
import spss, spssaux
vard = spssaux.VariableDict(variableType="numeric")
spss.SetMacroValue("!numerics", " ".join(vard.variables))
end program.


HTH,
Jon Peck

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Staffan Lindberg
Sent: Saturday, March 08, 2008 7:07 AM
To: [hidden email]
Subject: [SPSSX-L] Rearranging a large number of variables with a mix of numeric and non-numeric variables

Dear list!

I'm posing this question for a collegue of mine:

He has a file with the following set of variables:

1. BR (Crime=Numeric)
2. PN (Penalty=Numeric)
3. TI  (Timer=Time format)
4. CO (Comment=String format)

The file is arranged as follows

BR1, PN1, TI1, CO1, BR2, PN2, TI2, CO2,............................,BR2081,
PN2081, TI2081, CO2081.

Now he wants to do some transformations on the numeric variables, for
instance:

RECODE BR1, BR2, .........................................., BR2081
(Something).

Does he have to specify each BR-variable or is there a simpler way?
Obviously he can't use the TO-convention since the variables are not
consecutive. Rearranging them would also take an extrordinary amount of
time. Is this solvable?

best

Staffan Lindberg
Sweden

=====================
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
Reply | Threaded
Open this post in threaded view
|

SV: Rearranging a large number of variables with a mix of numeric and non-numeric variables

Staffan Lindberg
Thank you ever so much Jon and Wilhelm!

You made a distressed man very happy. Sorting by name in SPSS 16 though
seems to take a very long time indeed.

best

Staffan

-----Ursprungligt meddelande-----
Från: SPSSX(r) Discussion [mailto:[hidden email]] För Peck, Jon
Skickat: den 8 mars 2008 16:52
Till: [hidden email]
Ämne: Re: Rearranging a large number of variables with a mix of numeric and
non-numeric variables


With SPSS 16, you can sort the variables in a dataset according to their
names or any of their attributes, including type.

With any version since SPSS 14, programmability can be used to select sets
of variables based on name wildcards or to reorder the dataset variables.

Here is an example of sorting by name (the sort is case sensitive, which
could be changed if needed.)  Then TO could be used in the recode.

begin program.
import spss, spssaux
vard = spssaux.VariableDict()
alpha = sorted(vard.variables)
spss.Submit("ADD FILES FILE=* /KEEP=%s" % " ".join(alpha))
end program.

Here is an example that defines a macro listing all the numeric variables
that could be used in RECODE, for example.

begin program
import spss, spssaux
vard = spssaux.VariableDict(variableType="numeric")
spss.SetMacroValue("!numerics", " ".join(vard.variables))
end program.


HTH,
Jon Peck

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
Staffan Lindberg
Sent: Saturday, March 08, 2008 7:07 AM
To: [hidden email]
Subject: [SPSSX-L] Rearranging a large number of variables with a mix of
numeric and non-numeric variables

Dear list!

I'm posing this question for a collegue of mine:

He has a file with the following set of variables:

1. BR (Crime=Numeric)
2. PN (Penalty=Numeric)
3. TI  (Timer=Time format)
4. CO (Comment=String format)

The file is arranged as follows

BR1, PN1, TI1, CO1, BR2, PN2, TI2, CO2,............................,BR2081,
PN2081, TI2081, CO2081.

Now he wants to do some transformations on the numeric variables, for
instance:

RECODE BR1, BR2, .........................................., BR2081
(Something).

Does he have to specify each BR-variable or is there a simpler way?
Obviously he can't use the TO-convention since the variables are not
consecutive. Rearranging them would also take an extrordinary amount of
time. Is this solvable?

best

Staffan Lindberg
Sweden

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

=====================
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
Reply | Threaded
Open this post in threaded view
|

Re: Rearranging a large number of variables with a mix of numeric and non-numeric variables

Marks, Jim
In reply to this post by Staffan Lindberg
If you export the list of variable names to a spreadsheet, you can sort
alphabetically.

Then you can copy and paste into a syntax file and use ADD FILES to
reorder the file.

The rearranging will take a manageable amount of time.

--jim

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
Staffan Lindberg
Sent: Saturday, March 08, 2008 8:07 AM
To: [hidden email]
Subject: Rearranging a large number of variables with a mix of numeric
and non-numeric variables

Dear list!

I'm posing this question for a collegue of mine:

He has a file with the following set of variables:

1. BR (Crime=Numeric)
2. PN (Penalty=Numeric)
3. TI  (Timer=Time format)
4. CO (Comment=String format)

The file is arranged as follows

BR1, PN1, TI1, CO1, BR2, PN2, TI2,
CO2,............................,BR2081,
PN2081, TI2081, CO2081.

Now he wants to do some transformations on the numeric variables, for
instance:

RECODE BR1, BR2, .........................................., BR2081
(Something).

Does he have to specify each BR-variable or is there a simpler way?
Obviously he can't use the TO-convention since the variables are not
consecutive. Rearranging them would also take an extrordinary amount of
time. Is this solvable?

best

Staffan Lindberg
Sweden

=====================
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
Reply | Threaded
Open this post in threaded view
|

SV: Rearranging a large number of variables with a mix of numeric and non-numeric variables

Staffan Lindberg
Thanks Jim!

Seems like a nice solution. Problem is solved as I sorted the file for him
with "Select Variables" in SPSS 16. However it took appr. 7 hours (he had
more than 5.000 variables) Any improvement on that is very welcome.

best

Staffan

-----Ursprungligt meddelande-----
Från: Marks, Jim [mailto:[hidden email]]
Skickat: den 12 mars 2008 21:05
Till: Staffan Lindberg; [hidden email]
Ämne: RE: Rearranging a large number of variables with a mix of numeric and
non-numeric variables


If you export the list of variable names to a spreadsheet, you can sort
alphabetically.

Then you can copy and paste into a syntax file and use ADD FILES to reorder
the file.

The rearranging will take a manageable amount of time.

--jim

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
Staffan Lindberg
Sent: Saturday, March 08, 2008 8:07 AM
To: [hidden email]
Subject: Rearranging a large number of variables with a mix of numeric and
non-numeric variables

Dear list!

I'm posing this question for a collegue of mine:

He has a file with the following set of variables:

1. BR (Crime=Numeric)
2. PN (Penalty=Numeric)
3. TI  (Timer=Time format)
4. CO (Comment=String format)

The file is arranged as follows

BR1, PN1, TI1, CO1, BR2, PN2, TI2, CO2,............................,BR2081,
PN2081, TI2081, CO2081.

Now he wants to do some transformations on the numeric variables, for
instance:

RECODE BR1, BR2, .........................................., BR2081
(Something).

Does he have to specify each BR-variable or is there a simpler way?
Obviously he can't use the TO-convention since the variables are not
consecutive. Rearranging them would also take an extrordinary amount of
time. Is this solvable?

best

Staffan Lindberg
Sweden

=====================
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
Reply | Threaded
Open this post in threaded view
|

SV: Rearranging a large number of variables with a mix of numeric and non-numeric variables

Staffan Lindberg
In reply to this post by Marks, Jim
Ooops!

Sorry, I meant "Sort variables" of course (by name).

best

Staffan



Thanks Jim!

Seems like a nice solution. Problem is solved as I sorted the file for him
with "Select Variables" in SPSS 16. However it took appr. 7 hours (he had
more than 5.000 variables) Any improvement on that is very welcome.

best

Staffan

-----Ursprungligt meddelande-----
Från: Marks, Jim [mailto:[hidden email]]
Skickat: den 12 mars 2008 21:05
Till: Staffan Lindberg; [hidden email]
Ämne: RE: Rearranging a large number of variables with a mix of numeric and
non-numeric variables


If you export the list of variable names to a spreadsheet, you can sort
alphabetically.

Then you can copy and paste into a syntax file and use ADD FILES to reorder
the file.

The rearranging will take a manageable amount of time.

--jim

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
Staffan Lindberg
Sent: Saturday, March 08, 2008 8:07 AM
To: [hidden email]
Subject: Rearranging a large number of variables with a mix of numeric and
non-numeric variables

Dear list!

I'm posing this question for a collegue of mine:

He has a file with the following set of variables:

1. BR (Crime=Numeric)
2. PN (Penalty=Numeric)
3. TI  (Timer=Time format)
4. CO (Comment=String format)

The file is arranged as follows

BR1, PN1, TI1, CO1, BR2, PN2, TI2, CO2,............................,BR2081,
PN2081, TI2081, CO2081.

Now he wants to do some transformations on the numeric variables, for
instance:

RECODE BR1, BR2, .........................................., BR2081
(Something).

Does he have to specify each BR-variable or is there a simpler way?
Obviously he can't use the TO-convention since the variables are not
consecutive. Rearranging them would also take an extrordinary amount of
time. Is this solvable?

best

Staffan Lindberg
Sweden

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