Converting a Macro to a Python Program

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

Converting a Macro to a Python Program

David Futrell
Hi Everyone: 

I've just started working with Python and am hoping that one of you can accelerate my learning by helping me with what is probably a very simple Python operation. 


The following macro reads a SAV file that has employee id numbers as rows. There are 9 columns (g4id1 to g4id9) that contain a value that indicates which supervisors the employee falls under within our organizational hierarchy. In this case, there are 927 supervisors, but this value varies from quarter to quarter and I'd really like to calculate it have Python use it as the final value in the loop instead of my having to hard-code it.  


DEFINE !REPORTMACRO().
!DO !I=1 !TO 927
temporary. 
select if any(!I,g4id1 to g4id9). 
compute report_owner=!I. 
save outfile=!QUOTE(!CONCAT("R:\TEMP\ID ",!I,".sav."))
/keep report_owner empid. 
!DOEND 

!ENDDEFINE
!REPORTMACRO.  



The macro then expands and create 927 individual files, each containing the "report owner" and all the employee ids from the
employees who fall under him or her in the organization. 

The next step is to add all these files so the end result is a file which contains two columns....one with a variable
identifying the "report owner,"and the second column containing the employee ids that belong to him or her. 


Since "Add Files" is limited to 50 files, I have to go through this process: 

add files
 file='R:\TEMP\ID 1.SAV'
/file='R:\TEMP\ID 2.SAV'
.
.
.
/file='R:\TEMP\ID 49.SAV'
/file='R:\TEMP\ID 50.SAV'. 

save outfile='R:\TEMP\first50.SAV'. 

add files
 file='R:\TEMP\ID 51.SAV'
/file='R:\TEMP\ID 52.SAV'
.
.
.
/file='R:\TEMP\ID 99.SAV'
/file='R:\TEMP\ID 100.sav'. 

save outfile='R:\TEMP\file 51-100.sav'.

etc., until I get to the last files...

add files
 file='R:\TEMP\ID 901.SAV'
/file='R:\TEMP\ID 902.SAV'
.
.
.
/file='R:\TEMP\ID 926.SAV'
/file='R:\TEMP\ID 927.SAV'. 

save outfile='R:\TEMP\file 901-927.sav'.

Then I add up all of the intermediate files: 


add files 
 file='R:\TEMP\first50.SAV' 
/file='R:\TEMP\file 51-100.sav'
/file='R:\TEMP\file 101-150.sav'
/file='R:\TEMP\file 151-200.sav'
/file='R:\TEMP\file 201-250.sav' 
/file='R:\TEMP\file 251-300.sav' 
/file='R:\TEMP\file 301-350.sav'
/file='R:\TEMP\file 351-400.sav' 
/file='R:\TEMP\file 401-450.sav'
/file='R:\TEMP\file 451-500.sav' 
/file='R:\TEMP\file 501-550.sav' 
/file='R:\TEMP\file 551-600.sav' 
/file='R:\TEMP\file 601-650.sav' 
/file='R:\TEMP\file 651-700.sav'
/file='R:\TEMP\file 701-750.sav'
/file='R:\TEMP\file 751-800.sav'
/file='R:\TEMP\file 801-850.sav'
/file='R:\TEMP\file 851-900.sav' 
/file='R:\TEMP\file 901-927.sav'.



While this brute-force approach works just fine (although it takes about 25 minutes to run), I'm certain that there has to be a better way to do this with Python. 

Any advice?

David Futrell, Ph.D.
Senior Workforce Research Consultant
Eli Lilly and Company


Reply | Threaded
Open this post in threaded view
|

Re: Converting a Macro to a Python Program

Ruben Geert van den Berg
Dear David,

I also just started learning some very basic Python and I'm already a fan!

However, I found it hard to understand what you're trying to accomplish. You start with one .sav file, create 927 new .sav files and then add them up to one .sav file again. I probably misunderstand your exact situation but from what you posted I didn't see any necessity to do so.

I guess that g4id1 to g4id9 can contain more than one value from (1-927), right? Because if not, a solution would be

compute report_owner=0.
do repeat a=1 to 927.
if any(a,g4id1 to g4id9) report_owner=a.
end repeat.
execute.

If g4id1 to g4id9 can contain more than one value from (1-927) then one record in the original file becomes (number of different values between 1-927 within g4id1 to g4id9) record in the new file with an obvious maximum of 9. This can be easily accomplished by an XSAVE command placed in a loop (with thanks to David Marso). See the code below.

If this doesn't help, then please try to repost your situation with a more detailed explanation of what you wish to accomplish, OK?

HTH,

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

*Create testdata.

set seed 1.
dataset close all.

data list free/id.
begin data
1 2 3 4 5
end data.

dataset name d1.

do repeat a=g4id1 to g4id9.
compute a=rnd(rv.uni(.5,10.5)).
end repeat.
execute.

*Use xsave.

loop #i=1 to 10.
do if any(#i,g4id1 to g4id9).
compute report_owner=#i.
xsave outfile='c:\temp\final_file.sav'.
end if.
end loop.
execute.

delete variables report_owner.

*Visually inspect the new file.

get file='c:\temp\final_file.sav'.



Date: Thu, 17 Jun 2010 08:50:28 -0700
From: [hidden email]
Subject: Converting a Macro to a Python Program
To: [hidden email]

Hi Everyone: 

I've just started working with Python and am hoping that one of you can accelerate my learning by helping me with what is probably a very simple Python operation. 


The following macro reads a SAV file that has employee id numbers as rows. There are 9 columns (g4id1 to g4id9) that contain a value that indicates which supervisors the employee falls under within our organizational hierarchy. In this case, there are 927 supervisors, but this value varies from quarter to quarter and I'd really like to calculate it have Python use it as the final value in the loop instead of my having to hard-code it.  


DEFINE !REPORTMACRO().
!DO !I=1 !TO 927
temporary. 
select if any(!I,g4id1 to g4id9). 
compute report_owner=!I. 
save outfile=!QUOTE(!CONCAT("R:\TEMP\ID ",!I,".sav."))
/keep report_owner empid. 
!DOEND 

!ENDDEFINE
!REPORTMACRO.  



The macro then expands and create 927 individual files, each containing the "report owner" and all the employee ids from the
employees who fall under him or her in the organization. 

The next step is to add all these files so the end result is a file which contains two columns....one with a variable
identifying the "report owner,"and the second column containing the employee ids that belong to him or her. 


Since "Add Files" is limited to 50 files, I have to go through this process: 

add files
 file='R:\TEMP\ID 1.SAV'
/file='R:\TEMP\ID 2.SAV'
.
.
.
/file='R:\TEMP\ID 49.SAV'
/file='R:\TEMP\ID 50.SAV'. 

save outfile='R:\TEMP\first50.SAV'. 

add files
 file='R:\TEMP\ID 51.SAV'
/file='R:\TEMP\ID 52.SAV'
.
.
.
/file='R:\TEMP\ID 99.SAV'
/file='R:\TEMP\ID 100.sav'. 

save outfile='R:\TEMP\file 51-100.sav'.

etc., until I get to the last files...

add files
 file='R:\TEMP\ID 901.SAV'
/file='R:\TEMP\ID 902.SAV'
.
.
.
/file='R:\TEMP\ID 926.SAV'
/file='R:\TEMP\ID 927.SAV'. 

save outfile='R:\TEMP\file 901-927.sav'.

Then I add up all of the intermediate files: 


add files 
 file='R:\TEMP\first50.SAV' 
/file='R:\TEMP\file 51-100.sav'
/file='R:\TEMP\file 101-150.sav'
/file='R:\TEMP\file 151-200.sav'
/file='R:\TEMP\file 201-250.sav' 
/file='R:\TEMP\file 251-300.sav' 
/file='R:\TEMP\file 301-350.sav'
/file='R:\TEMP\file 351-400.sav' 
/file='R:\TEMP\file 401-450.sav'
/file='R:\TEMP\file 451-500.sav' 
/file='R:\TEMP\file 501-550.sav' 
/file='R:\TEMP\file 551-600.sav' 
/file='R:\TEMP\file 601-650.sav' 
/file='R:\TEMP\file 651-700.sav'
/file='R:\TEMP\file 701-750.sav'
/file='R:\TEMP\file 751-800.sav'
/file='R:\TEMP\file 801-850.sav'
/file='R:\TEMP\file 851-900.sav' 
/file='R:\TEMP\file 901-927.sav'.



While this brute-force approach works just fine (although it takes about 25 minutes to run), I'm certain that there has to be a better way to do this with Python. 

Any advice?

David Futrell, Ph.D.
Senior Workforce Research Consultant
Eli Lilly and Company




New Windows 7: Find the right PC for you. Learn more.
Reply | Threaded
Open this post in threaded view
|

Automatic reply: Converting a Macro to a Python Program

Ling Ting
I am out of the office until June 23rd. If you need immediate assistance please contact 479-575-2905.

Thank you.

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