For each sheet a new dataset

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

For each sheet a new dataset

Esteban Sanjuan
Hi all,

I have a excel-file in which the data is divided into many sheets. I
want do have for each sheet a dataset. As you surely know looks the
syntax-command like that:

get data
   /type=xls
   /file='C:\...\example.xls'
   /sheet=index n
   /readnames=on
   /assumedstrwidth=32767.
dataset name sheetn.

Does anyone know, if there's a possibility to write a script, that runs
a loop (for n) and opens for each sheet in the selected excel-file a new
dataset?

I'm grateful for any help!

=====================
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: For each sheet a new dataset

Raynald Levesque-2
Hi

This is very easy to do using either a macro or Python.

I assume the file 'C:\temp\test.xls' has data in its first 3 sheets.

* A macro solution.

DEFINE !getData(n=!TOKENS(1))
!DO !cnt=1 !TO !n
get data
  /type=xls
  /file='C:\temp\test.xls'
  /sheet=index !cnt
  /readnames=on
  /assumedstrwidth=32767.
dataset name !CONCAT('sheet',!cnt).
!DOEND
!ENDDEFINE.

SET MPRINT=YES.
DATASET CLOSE ALL.
!getData n=3.


* Python version of the above macro.

BEGIN PROGRAM.
import spss
def getData(n):
    for cnt in range(n):
        cmd=r"""
        get data
          /type=xls
          /file='C:\temp\test.xls'
          /sheet=index %u
          /readnames=on
          /assumedstrwidth=32767.
        dataset name sheet%u. """ % (cnt + 1,cnt + 1)
        spss.Submit(cmd)
getData(3)
END PROGRAM.

Regards,

--
Raynald Levesque
www.spsstools.net



On Dec 12, 2007 9:29 AM, Esteban Sanjuan <[hidden email]>
wrote:

> Hi all,
>
> I have a excel-file in which the data is divided into many sheets. I
> want do have for each sheet a dataset. As you surely know looks the
> syntax-command like that:
>
> get data
>   /type=xls
>   /file='C:\...\example.xls'
>   /sheet=index n
>   /readnames=on
>   /assumedstrwidth=32767.
> dataset name sheetn.
>
> Does anyone know, if there's a possibility to write a script, that runs
> a loop (for n) and opens for each sheet in the selected excel-file a new
> dataset?
>
> I'm grateful for any help!
>
> =====================
> 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