A Do loop that changes part of a variable name and dataset name?

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

A Do loop that changes part of a variable name and dataset name?

Jess-41
Hi All,

I'm new to SPSS programming (but do know SAS) and I haven't been able to
find the right code to do the following:

I have separate datasets for surveys done in multiple years. The same
variables are named the same thing on each dataset. I'd like to create a
program that allows me to create (recode the data) numerators and
denominators for each year's data in each years file. So, I'd want to loop
through each dataset, creating variables and naming them with the year
convention at the end like this (var_n_03 var_d_03 in the dataset_03) and
(var_n_08 var_d_08 in the  dataset_08). I've got hundreds of variables to be
created over multiple years so a macro that loops through years would be
great. What I don't understand is how to do the looping so I change the
variable name and call the correct year (dataset name)? Most language I've
found here uses the loops to change the variable, not a part of it. (Maybe I
don't know how to search for the language correctly)

Any help would be great, Thanks!

=====================
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: A Do loop that changes part of a variable name and dataset name?

Albert-Jan Roskam
hi!

Untested Python solution:

BEGIN PROGRAM.

import glob, re
import spss
for sav in glob.glob("c:/temp/*.sav"):
        year = re.search("\d+", sav).group(0)
        spss.Submit("get file = '%s'." % sav)
        spss.Submit("compute var_n_%2d = somevar eq 1." % year)
        spss.Submit("compute var_d_%2d = othervar eq 1." % year)
        spss.Submit("save outfile = '%s_modified.sav'." % sav[:-4])

END PROGRAM.

Note: (1) This assumes that the only digits in the file names denote the year. (2) the compute statements should be adjusted to your needs. You could also use RECODE ... INTO  instead. (3) This also assumes that all savs reside in  one dir, and all savs in that dir need to be modified. You could adjust the glob to narrow the pattern [e.g. glob.glob("c:/temp/data_*.sav")]

Cheers & have a good weekend!

Albert-Jan

--- On Thu, 9/17/09, Jess <[hidden email]> wrote:

> From: Jess <[hidden email]>
> Subject: [SPSSX-L] A Do loop that changes part of a variable name and              dataset name?
> To: [hidden email]
> Date: Thursday, September 17, 2009, 11:18 PM
> Hi All,
>
> I'm new to SPSS programming (but do know SAS) and I haven't
> been able to
> find the right code to do the following:
>
> I have separate datasets for surveys done in multiple
> years. The same
> variables are named the same thing on each dataset. I'd
> like to create a
> program that allows me to create (recode the data)
> numerators and
> denominators for each year's data in each years file. So,
> I'd want to loop
> through each dataset, creating variables and naming them
> with the year
> convention at the end like this (var_n_03 var_d_03 in the
> dataset_03) and
> (var_n_08 var_d_08 in the  dataset_08). I've got
> hundreds of variables to be
> created over multiple years so a macro that loops through
> years would be
> great. What I don't understand is how to do the looping so
> I change the
> variable name and call the correct year (dataset name)?
> Most language I've
> found here uses the loops to change the variable, not a
> part of it. (Maybe I
> don't know how to search for the language correctly)
>
> Any help would be great, Thanks!
>
> =====================
> 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