|
I have an excel file which contains multiple worksheets - one for each year 1970-2008 - and would like to read each of these sheets and store as one data file. The data within each of the worksheets is in the same format. As it does not have a separate variable for the year so would also like to add a variable with value equal to the worksheet name. I could read each worksheet separately, compute the new variable, save the file and then add all the files eg
GET DATA /TYPE=XLS /FILE='C:\data_70-08.xls' /SHEET=name '70m' /CELLRANGE=range 'A3:E115' /READNAMES=on /ASSUMEDSTRWIDTH=32767. string name (a4). compute name = '70m'. save outfile = t70m. GET DATA /TYPE=XLS /FILE='C:\data_70-08.xls' /SHEET=name '71m' /CELLRANGE=range 'A3:E115' /READNAMES=on /ASSUMEDSTRWIDTH=32767. string name (a4). compute name = '71m'. save outfile = t71m. etc ... get file = t70m. add files file = * / file = t71m ... etc This is a bit tedious and I was hoping that I could automate the procedure in some way. Any suggestions would be greatly appreciated. Thanks Valerie ===================== 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 |
|
|
Administrator
|
In reply to this post by Valerie Seagroatt
It seems to me you should be able to do this with a good old-fashioned macro. Something like this (untested):
define !myget ( slist !cmdend ) !do !sheet !in (!slist) !let !sname = !quote( !concat(!sheet,'m') ) !let !dsname = !concat('data_',!sheet) GET DATA /TYPE=XLS /FILE='C:\data_70-08.xls' /SHEET=name !sname /CELLRANGE=range 'A3:E115' /READNAMES=on /ASSUMEDSTRWIDTH=32767. string name (a4). compute name = !sname . exe. dataset name !dsname . !doend !enddefine . When calling it, you hand it your list of worksheet numbers in string format -- e.g., '70' '71' ... '08'. After reading in all the sheets, merge them via ADD FILES, as shown in Albert-Jan's response.
--
Bruce Weaver bweaver@lakeheadu.ca http://sites.google.com/a/lakeheadu.ca/bweaver/ "When all else fails, RTFM." PLEASE NOTE THE FOLLOWING: 1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above. 2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (https://listserv.uga.edu/). |
|
Although nothing beats Python, I wrote a very ugly one for my colleague that does the add files command as well. I just put the macro arguments in the body which was just a matter of laziness. I find this syntax rather tricky (the dataset name trick generates warnings) but the bottom line: it created the desired dataset. Since a number of string variables had differing lengths over sheets, I was very grateful that 'alter type' was introduced in v16 because the previous version (written for v14) was even clumsier.
define !Ester() !do !l1=1 !to 12 get data /type=xls /file='NIPOBase Business_en _Consumer_respons 2009.xls' /sheet=index !l1 /readnames=on. compute month =!l1. alter type Veldwerknr Omschrijving Projectleider Steekproefbron(a1000). !if (!l1=1) !then dataset name d1. !else add files file * /file d1. execute. !ifend dataset close all. dataset name d1. !doend alter type Veldwerknr Omschrijving Projectleider Steekproefbron(amin). !enddefine. Ruben van den Berg Consultant Models & Methods TNS NIPO Email: [hidden email] Mobiel: +31 6 24641435 Telefoon: +31 20 522 5738 Internet: www.tns-nipo.com > Date: Mon, 31 May 2010 04:45:23 -0700 > From: [hidden email] > Subject: Re: Reading multiple sheets of excel file > To: [hidden email] > > It seems to me you should be able to do this with a good old-fashioned macro. > Something like this (untested): > > define !myget ( slist !cmdend ) > > !do !sheet !in (!slist) > > !let !sname = !quote( !concat(!sheet,'m') ) > !let !dsname = !concat('data_',!sheet) > > GET DATA /TYPE=XLS > /FILE='C:\data_70-08.xls' > /SHEET=name !sname > /CELLRANGE=range 'A3:E115' > /READNAMES=on > /ASSUMEDSTRWIDTH=32767. > string name (a4). > compute name = !sname . > exe. > dataset name !dsname . > > !doend > > !enddefine . > > When calling it, you hand it your list of worksheet numbers in string format > -- e.g., '70' '71' ... '08'. After reading in all the sheets, merge them > via ADD FILES, as shown in Albert-Jan's response. > > > > > Valerie Seagroatt wrote: > > > > I have an excel file which contains multiple worksheets - one for each > > year 1970-2008 - and would like to read each of these sheets and store as > > one data file. The data within each of the worksheets is in the same > > format. As it does not have a separate variable for the year so would also > > like to add a variable with value equal to the worksheet name. I could > > read each worksheet separately, compute the new variable, save the file > > and then add all the files eg > > > > GET DATA /TYPE=XLS > > /FILE='C:\data_70-08.xls' > > /SHEET=name '70m' > > /CELLRANGE=range 'A3:E115' > > /READNAMES=on > > /ASSUMEDSTRWIDTH=32767. > > string name (a4). > > compute name = '70m'. > > save outfile = t70m. > > > > GET DATA /TYPE=XLS > > /FILE='C:\data_70-08.xls' > > /SHEET=name '71m' > > /CELLRANGE=range 'A3:E115' > > /READNAMES=on > > /ASSUMEDSTRWIDTH=32767. > > string name (a4). > > compute name = '71m'. > > save outfile = t71m. > > etc ... > > > > get file = t70m. > > add files file = * / file = t71m ... etc > > > > > > This is a bit tedious and I was hoping that I could automate the procedure > > in some way. Any suggestions would be greatly appreciated. > > > > Thanks > > > > Valerie > > > > ===================== > > 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 > > > > > > > ----- > -- > Bruce Weaver > [hidden email] > http://sites.google.com/a/lakeheadu.ca/bweaver/ > "When all else fails, RTFM." > > NOTE: My Hotmail account is not monitored regularly. > To send me an e-mail, please use the address shown above. > -- > View this message in context: http://old.nabble.com/Reading-multiple-sheets-of-excel-file-tp28716025p28730119.html > Sent from the SPSSX Discussion mailing list archive at Nabble.com. > > ===================== > 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 New Windows 7: Simplify what you do everyday. Find the right PC for you. |
| Free forum by Nabble | Edit this page |
