So many years ago I was adept with Pascal. This, like so many other languages could handle procedures and functions, and both could handle parameters. Can SPSS work with procedures and parameters?
I have just completed some code, which works well enough. The syntax is simple. It reads data using GET FILE, carries out some computes, aggregates the data, and stores it as a dataset. There are several steps and several datasets, at the end of which I use MATCH FILES to assemble all of the data into the way I want it. Neatly spaced out the code occupies about 150 lines. As noted, it works okay, but my issue is that that was for one year. I have identical data files for six years. I could simply copy and paste the code five more times and edit the syntax changing the various file names for each of the subsequent data files I wish to use. That would work. My experience with Pascal (I don’t know any other language) tells me that I could make my code a procedure and call it from the main body of the program as many times as necessary and pass various values as parameters. The only parameter I think I would need to pass would be Year. Each time the procedure is run it would use GET FILE and the path would include the parameter value, so reading a different data file each time. The parameter value would also be included in a compute statement to assign it to a variable (probably named Year). |
Administrator
|
It sounds like you want a macro with arguments for the variables that can change--something like this (untested), for example:
DEFINE !MyMacro ( V1 !charend('/') / V2 !charend('/') / V3 !charend('/') / V4 !cmdend ) /* Insert your code here. */ /* When referring to V1-V4, start with exclamation point: */ /* e.g., !V1, !V2, !V3, !V4. */ /* For example: */ FREQUENCIES !V1 !V2. DESCRIPTIVES !V3 !V4. !ENDDEFINE. Then call the macro like this: !MyMacro V1 = A1 / V2 = A2 / V3 = A3 / V4 = A4 . * Again, with another set of variables. !MyMacro V1 = B1 / V2 = B2 / V3 = B3 / V4 = B4 . * Etc. For more info, take a look at DEFINE-!ENDDEFINE in the command syntax reference manual. See also the following links: http://web.uvic.ca/~cass/spss-macros.html http://spsstools.net/de/learning-macros/ HTH.
--
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/). |
That looks like it could be the ticket. And if not, it will certainly be interesting to look at it. Thank you for pointing it out.
|
Macro is okay for simple parameter substitution, but if you want more programming power, including the ability to work with metadata, conditional logic, subroutines/functions etc, I would urge you to invest some time in Python programmability with Statistics. The Programming and Data Management book available from the SPSS Comminity or Predictive Analytics website has many examples of using this approach.
On Monday, February 8, 2016, Ron0z <[hidden email]> wrote: That looks like it could be the ticket. And if not, it will certainly be -- ===================== 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 |
Thank you Jon. I'll check that out.
|
In reply to this post by Ron0z
You are asking how you make SPSS function the way that you are
=====================
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
accustomed to seeing Pascal; that is probably the wrong approach. If this is a one-shot thing ... go with brute force and make the 6 versions. If you want to do it the easy way, learn a few of the available SPSS facilities and consider how to use files and directories more effectively. Here are some options that come to mind, for how I might solve this one problem. 1a) Use the File Handle to define the files. Then you just need to change the few lines of File Handles at the start, in order to do a new run. 1b) Use one or more MACROs that have the file names, and just change what is in the MACRO between runs. 2) Use Include or Insert File to pull in the file definitions, as File Handle or other. Each of 6 files of lines pulled in has the file definitions for a different run. All you need to change, between runs, is the name of the file to insert (safer), or else the contents of the particular file being inserted (not as safe). 3) Put all of the data for all years into one file, with Year as an identifier. This gives you one run for everything. You can Split Files on a file sorted by year and get a run that does particular things for each year. 4) Put all the data for one year into its own directory, and name all the files the same. Run your run separately in the different directories, after CD to fine the directory. (I think that still works.) All of those, above, are solutions that give code that is easier to follow and maintain by programmers who may not know Python. Python gives some great power, but perhaps you should learn, first, what can be done with the native SPSS facilities. -- Rich Ulrich > Date: Mon, 8 Feb 2016 18:01:42 -0700 > From: [hidden email] > Subject: Can SPSS work with procedures and parameters? > To: [hidden email] > > So many years ago I was adept with Pascal. This, like so many other > languages could handle procedures and functions, and both could handle > parameters. Can SPSS work with procedures and parameters? > > I have just completed some code, which works well enough. The syntax is > simple. It reads data using GET FILE, carries out some computes, aggregates > the data, and stores it as a dataset. There are several steps and several > datasets, at the end of which I use MATCH FILES to assemble all of the data > into the way I want it. Neatly spaced out the code occupies about 150 > lines. > > As noted, it works okay, but my issue is that that was for one year. I > have identical data files for six years. I could simply copy and paste the > code five more times and edit the syntax changing the various file names for > each of the subsequent data files I wish to use. That would work. > > My experience with Pascal (I don’t know any other language) tells me that I > could make my code a procedure and call it from the main body of the program > as many times as necessary and pass various values as parameters. The only > parameter I think I would need to pass would be Year. Each time the > procedure is run it would use GET FILE and the path would include the > parameter value, so reading a different data file each time. The parameter > value would also be included in a compute statement to assign it to a > variable (probably named Year). > > |
Administrator
|
Good suggestions, Rich. I especially like number 3, one file with Year as a variable. As you note, one can use SPLIT FILE, or compute filter variables based on the year, etc.
And your final point about exploring native SPSS (NPR*) options first is an excellent one. * No Python Required
--
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/). |
Administrator
|
In reply to this post by Bruce Weaver
If you are a tricky SOB (like me) you can even get them to call arbitrary sub-macros, do recursion and spit back return values. However, they are really crappy at doing even elementary math. Dumb as a sack of bricks in that regard.
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me. --- "Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis." Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?" |
Administrator
|
Demo of those ideas ;-).
DEFINE !S (!POS !TOKENS(1) / !POS !TOKENS(1) ) ECHO "running S". ECHO !QUOTE(!1). ECHO !QUOTE(!2). !ENDDEFINE. DEFINE !T ( ) ECHO "Running T". !ENDDEFINE. Define !R (!POS !CHAREND ("/") / !POS !CHAREND ("/") /!POS !CHAREND ("/") /!POS !CMDEND ) !IF (!1 !NE "") !THEN !SUBSTR(!UNQUOTE(!3),3) !HEAD(!1) !HEAD(!2) . !R !TAIL (!1) / !TAIL(!2) / !3 / !4 . !ELSE !SUBSTR(!UNQUOTE(!4),3). ECHO 'Done in R'. !IFEND !ENDDEFINE. SET MPRINT ON PRINTBACK ON. !R a b c / d e f / '/*!S' / '/*!T' .
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me. --- "Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis." Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?" |
In reply to this post by Ron0z
I read through the section on Macros. I think this is the answer to my problem. Possibly when I get some time I might look into Python to see what it has to offer, but this is exactly what I was looking for. I have something running, and I’m very happy. I’m very obliged to everyone who helped me. Though, the syntax had me stumped somewhat. (I think I’ll need the text next to me for a while.) The syntax is unusual; quite different from my experience with Pascal. I’ll cope. SPSS is also a bit quirky with macros. Let me explain.
I came across an unusual problem in my endeavours. The use of parameters in the body of the macro (ie. between define and !enddefine) must be preceded by an exclamation point. My issue was that I had inadvertently excluded that detail. My code had a series of file handles to point to system files, and I was passing file handles to the macro. However, within the body of the macro I had failed to use exclamation points. The syntax used to call the macro was correct. The syntax in the macro parameter definition was also correct. However, in the body of the macro I had entered the following: match files /file = step2CurYr /in = inStep /table = list /in = inList /by ClientID degree. (note the missing !s) instead of: match files /file = !step2CurYr /in = inStep /table = !list /in = inList /by ClientID degree. SPSS reported that the file did not exist. Which was true. It took me ages to spot the error, but it was particularly difficult because even when I had found the error and corrected it, SPSS continued to report that the file did not exist despite there being no error in the script. Bizarre! The only way the error ceased to be reported was when I shut SPSS down and restarted it. The code then ran error free. |
Administrator
|
I know we're not even half way through February yet, but I think that's a pretty strong contender for the SPSSX-L Understatement of 2016 Award. ;-)
--
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/). |
Administrator
|
Ah come on Bruce,
<SNARK> What exactly do you find about macros that isn't completely intuitive and starkly tranceparent? Please illustrate with examples! </SNARK>
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me. --- "Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis." Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?" |
Administrator
|
Well, let's just say that if we had photos of Sy Sperling* before and after programming some SPSS macros, they'd look the opposite of his usual before & after photos.
* For those who don't get the reference, Sy Sperling was President (but also a client) of the Hair Club for Men back in the 80s. You can see his before & after photos (and an old TV commercial) here: http://www.wsj.com/articles/SB10001424053111903554904576458242510472566 http://si.wsj.net/public/resources/images/OB-OU753_SBsy1_E_20110720154557.jpg https://www.youtube.com/watch?v=IuRLGdGnqSU By the way, David, how recent is that profile pic of yours in Nabble? You appear to have retained all your hair, despite your frequent and extensive forays into macro programming. What's your secret? It makes one wonder if you have a Dorian-Gray-like portrait stashed in the attic. ;-)
--
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/). |
Administrator
|
In reply to this post by Ron0z
A few pointers.
Macros are utterly stupid. There is no substantial intelligence lurking beneath that beast. It can basically manipulate strings (and not a whole lot more). !CONCAT, !SUBSTR, !LENGTH... It has the mathematical equivalent of illiteracy when it comes to the comprehension of numeric values ( 10 < 2 ;-))) Note, even with your catch of the ! issue your code would still fail because macro symbols are limited to 8 characters (including the !) SET MPRINT ON and PRINTBACK ON are your friends (maybe even besties in debugging). Best when reporting bizarre behavior with MACRO is to post the complete (distilled) macro code, calling code and exact error messages. If you haven't already sacrificed what remains of your mind to the dark beast I would suggest acquiring python skills lest ye drive yourself mad with macrophilia. I could be the poster child for Exhibit A. OTOH: They will tear macro from my cold dead hand ;-))) Yeah, I have a tragic love/hate relationship with this antiquated beast. ---------------------------------------- !DEFINE !WHATS (THAT !CMDEND) !DO !THIS !IN (!THAT) !Screw What=!This !DOEND !ENDDEFINE . DEFINE !Screw (What !CmdEnd) !IF (!What = 'That') !THEN !Whats That = !What !ELSE !LET !THAT='That' !SCREW THAT=!THAT !IFEND !ENDDEFINE. !WHATS THAT=This That Is Not That Or This. -------------------------------------------------------------------------------
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me. --- "Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis." Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?" |
In reply to this post by Bruce Weaver
In the spring, before nesting season begins, bird pairs have been noticed to congregate around the places where people are working with spss macros in hopes of gathering torn-out and discarded hair for their nests.
-----Original Message----- From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Bruce Weaver Sent: Thursday, February 11, 2016 9:01 AM To: [hidden email] Subject: Re: Can SPSS work with procedures and parameters? Well, let's just say that if we had photos of Sy Sperling* before and after programming some SPSS macros, they'd look the /opposite/ of his usual before & after photos. * For those who don't get the reference, Sy Sperling was President (but also a client) of the Hair Club for Men back in the 80s. You can see his before & after photos (and an old TV commercial) here: http://www.wsj.com/articles/SB10001424053111903554904576458242510472566 http://si.wsj.net/public/resources/images/OB-OU753_SBsy1_E_20110720154557.jpg https://www.youtube.com/watch?v=IuRLGdGnqSU By the way, David, how recent is that profile pic of yours in Nabble? You appear to have retained all your hair, despite your frequent and extensive forays into macro programming. What's your secret? It makes one wonder if you have a Dorian-Gray-like portrait stashed in the attic. ;-) David Marso wrote > Ah come on Bruce, > <SNARK> > What exactly do you find about macros that isn't completely intuitive > and starkly tranceparent? Please illustrate with examples! > </SNARK> > > Bruce Weaver wrote >> >> Ron0z wrote >>> SPSS is also a bit quirky with macros. >> I know we're not even half way through February yet, but I think >> that's a pretty strong contender for the >> SPSSX-L Understatement of 2016 Award * >> . >> >> ;-) >> >> >> Ron0z wrote >>> I read through the section on Macros. I think this is the answer to >>> my problem. Possibly when I get some time I might look into Python >>> to see what it has to offer, but this is exactly what I was looking >>> for. I have something running, and I’m very happy. I’m very >>> obliged to everyone who helped me. Though, the syntax had me >>> stumped somewhat. (I think I’ll need the text next to me for a >>> while.) The syntax is unusual; quite different from my experience with Pascal. I’ll cope. >>> SPSS is also a bit quirky with macros. Let me explain. >>> >>> I came across an unusual problem in my endeavours. The use of >>> parameters in the body of the macro (ie. between define and >>> !enddefine) must be preceded by an exclamation point. My issue was >>> that I had inadvertently excluded that detail. My code had a series >>> of file handles to point to system files, and I was passing file >>> handles to the macro. However, within the body of the macro I had >>> failed to use exclamation points. The syntax used to call the macro >>> was correct. The syntax in the macro parameter definition was also >>> correct. However, in the body of the macro I had entered the following: >>> >>> match files /file = step2CurYr /in = inStep /table = list /in = >>> inList /by ClientID degree. >>> >>> (note the missing !s) instead of: >>> >>> match files /file = !step2CurYr /in = inStep /table = !list /in >>> = inList /by ClientID degree. >>> >>> SPSS reported that the file did not exist. Which was true. It took >>> me ages to spot the error, but it was particularly difficult because >>> even when I had found the error and corrected it, SPSS continued to >>> report that the file did not exist despite there being no error in the script. >>> Bizarre! The only way the error ceased to be reported was when I >>> shut SPSS down and restarted it. The code then ran error free. ----- -- 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://spssx-discussion.1045642.n5.nabble.com/Can-SPSS-work-with-procedures-and-parameters-tp5731470p5731496.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 ===================== 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
|
Gene, ;-)))
And close behind the birds are gathering a pack of pythons ready to spring and steal the eggs from the hairy nest. May they choke on them ;-) To answer Bruce's question: Still have the hair (older picture) - However the beard has become much more obviously grey (nothing a shave won't alleviate). I stopped pulling my hair out with macro years ago and it all finally grew back over time. ---
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me. --- "Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis." Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?" |
In reply to this post by David Marso
While I have done my best, with alas only partial success, to purge all knowledge of macro from my brain, I do not thing this statement is correct. "macro symbols are limited to 8 characters (including the !)" set mprint on. define !macrowithalongname () !let !amacrovariable = jobcat freq !amacrovariable. !enddefine. !macrowithalongname. * this one will fail. define !macrowithalongname () !let !amacrovariable = jobcat freq !amacrovariableXXX. !enddefine. !macrowithalongname. define !macrowithalongname () !let !amacrovariable = jobcat !do !anotherlongname = 1 !to 5 freq !amacrovariable. !doend. !enddefine. !macrowithalongname. On Thu, Feb 11, 2016 at 7:31 AM, David Marso <[hidden email]> wrote: A few pointers. |
Administrator
|
Oops, my bad. I was getting precaffienated crosstalk from the other ginger bearded bastard child (MATRIX).
Macros symbols have the same rules as SPSS variable names ;-)))
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me. --- "Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis." Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?" |
Free forum by Nabble | Edit this page |