All, I like being able to update the dates on my files using this
simple macro: DEFINE !filedate() '20111219' !ENDDEFINE. But now I have variable names that have a date embedded in
them. I want to update all instances of those variable names with something
like a macro. For example, a typical command might be: if (Sep_Oct_2011_credits = 1)
priordebitmatched_Sep_Oct_2011_credits_credits = 1. I want to change the month and date portions since they will
change every time I run the file, and I don’t want to have to convert all
the syntax to a macro. That seems overkill. Thanks Matt Matthew Pirritano, Ph.D. Research Analyst IV Medical Services Initiative (MSI) Orange County Health Care Agency (714) 568-5648 |
I'm out of the office until January 3rd.
Monday Dec. 19 through Wednesday Dec. 21, Ann Reed ([hidden email]) will be filling in for me. Thursday Dec. 22 and Friday Dec. 23, Mike O'Neil ([hidden email]) will be filling in for me. The week following Christmas
we'll be closed. Cheers, Bob
|
In reply to this post by mpirritano
The simplest and most transparent way may be the old-fashioned one -
Create a basic syntax file with a dummy date, e.g., XXX_XXX. Then, for the monthly processing, create the Report file, which will - 1) read your basic syntax file as a single Text variable per line; 2) use sub-string replacement in order to replace XXX_XXX with your desired date, e.g., Sep_Oct; 3) "write" out the file as Text and close it; 4) "Include" the new text file to run it. Each month, you change the desired date, and then run the Report. -- Rich Ulrich Date: Mon, 19 Dec 2011 12:57:25 -0800 From: [hidden email] Subject: macro to replace portions of variable names in syntax file To: [hidden email] All,
I like being able to update the dates on my files using this simple macro:
DEFINE !filedate() '20111219' !ENDDEFINE.
But now I have variable names that have a date embedded in them. I want to update all instances of those variable names with something like a macro.
For example, a typical command might be:
if (Sep_Oct_2011_credits = 1) priordebitmatched_Sep_Oct_2011_credits_credits = 1.
I want to change the month and date portions since they will change every time I run the file, and I don’t want to have to convert all the syntax to a macro. That seems overkill. [...] |
This could be done more easily using Python
regular expressions. You might open a template syntax file into a
syntax window; then run a small Python program to update the template to
the particular dates you want, and then run it from there. But this
problem appears to be a little more complicated. What is the logic
that would apply to names like _Sep_Oct_2011_credits_credits?
p.s. There is a function in spssaux2.py named CreateFileNameWDate that automatically picks up the current date in generating a file name. Regards, Jon Peck (no "h") aka Kim Senior Software Engineer, IBM [hidden email] new phone: 720-342-5621 From: Rich Ulrich <[hidden email]> To: [hidden email] Date: 12/19/2011 02:27 PM Subject: Re: [SPSSX-L] macro to replace portions of variable names in syntax file Sent by: "SPSSX(r) Discussion" <[hidden email]> The simplest and most transparent way may be the old-fashioned one - Create a basic syntax file with a dummy date, e.g., XXX_XXX. Then, for the monthly processing, create the Report file, which will - 1) read your basic syntax file as a single Text variable per line; 2) use sub-string replacement in order to replace XXX_XXX with your desired date, e.g., Sep_Oct; 3) "write" out the file as Text and close it; 4) "Include" the new text file to run it. Each month, you change the desired date, and then run the Report. -- Rich Ulrich Date: Mon, 19 Dec 2011 12:57:25 -0800 From: [hidden email] Subject: macro to replace portions of variable names in syntax file To: [hidden email] All, I like being able to update the dates on my files using this simple macro: DEFINE !filedate() '20111219' !ENDDEFINE. But now I have variable names that have a date embedded in them. I want to update all instances of those variable names with something like a macro. For example, a typical command might be: if (Sep_Oct_2011_credits = 1) priordebitmatched_Sep_Oct_2011_credits_credits = 1. I want to change the month and date portions since they will change every time I run the file, and I don’t want to have to convert all the syntax to a macro. That seems overkill. [...] |
Jon, Sep_Oct_2011_credits_credits should have had only one
credits. Typo. This is a growing file that I add a field to every time I match
a newer file of credits to it. So I need the accumulating records of what has
been credited each month. The “Sep_Oct_2011” part corresponds to
the file the credit came from. It’s not related to a current date. I could imagine that this would be easy to do in Python, but
since I want a variable that represents the date portion of the field to be
available throughout the syntax I’d have to include all of the syntax in a
python program, right? Or is there another way. Thanks Matt Matthew Pirritano, Ph.D. Research Analyst IV Medical Services Initiative (MSI) Orange County Health Care Agency (714) 568-5648 From: SPSSX(r)
Discussion [mailto:[hidden email]] On
Behalf Of Jon K Peck This could be done more easily using Python regular
expressions. You might open a template syntax file into a syntax window;
then run a small Python program to update the template to the particular dates
you want, and then run it from there. But this problem appears to be a
little more complicated. What is the logic that would apply to names like
_Sep_Oct_2011_credits_credits?
Date: Mon, 19 Dec 2011 12:57:25
-0800 |
You could include the whole program
in a Submit block, but there are other alternatives. What I was suggesting
is that you have a standard syntax file in a template format that you open
in a syntax window, and then you use a Python script to read the window
contents, substitute the required date specifications and write it back
to the same or a new window. So the syntax file itself has no programmability
code in it. The template file might contain strings like <MONTH><DAY><YEAR>
etc, and the Python code would substitute appropriately for these patterns.
You could also produce a small stand-alone Python program that takes input and output file specifications and just creates date-substituted syntax. The third approach would be to write all the syntax using macros for the dates and then define the macro to create those date strings as appropriate. Jon Peck (no "h") aka Kim Senior Software Engineer, IBM [hidden email] new phone: 720-342-5621 From: "Pirritano, Matthew" <[hidden email]> To: [hidden email] Date: 12/19/2011 04:18 PM Subject: Re: [SPSSX-L] macro to replace portions of variable names in syntax file Sent by: "SPSSX(r) Discussion" <[hidden email]> Jon, Sep_Oct_2011_credits_credits should have had only one credits. Typo. This is a growing file that I add a field to every time I match a newer file of credits to it. So I need the accumulating records of what has been credited each month. The “Sep_Oct_2011” part corresponds to the file the credit came from. It’s not related to a current date. I could imagine that this would be easy to do in Python, but since I want a variable that represents the date portion of the field to be available throughout the syntax I’d have to include all of the syntax in a python program, right? Or is there another way. Thanks Matt Matthew Pirritano, Ph.D. Research Analyst IV Medical Services Initiative (MSI) Orange County Health Care Agency (714) 568-5648 From: SPSSX(r) Discussion [[hidden email]] On Behalf Of Jon K Peck Sent: Monday, December 19, 2011 2:50 PM To: [hidden email] Subject: Re: macro to replace portions of variable names in syntax file This could be done more easily using Python regular expressions. You might open a template syntax file into a syntax window; then run a small Python program to update the template to the particular dates you want, and then run it from there. But this problem appears to be a little more complicated. What is the logic that would apply to names like _Sep_Oct_2011_credits_credits? p.s. There is a function in spssaux2.py named CreateFileNameWDate that automatically picks up the current date in generating a file name. Regards, Jon Peck (no "h") aka Kim Senior Software Engineer, IBM [hidden email] new phone: 720-342-5621 From: Rich Ulrich <[hidden email]> To: [hidden email] Date: 12/19/2011 02:27 PM Subject: Re: [SPSSX-L] macro to replace portions of variable names in syntax file Sent by: "SPSSX(r) Discussion" <[hidden email]> The simplest and most transparent way may be the old-fashioned one - Create a basic syntax file with a dummy date, e.g., XXX_XXX. Then, for the monthly processing, create the Report file, which will - 1) read your basic syntax file as a single Text variable per line; 2) use sub-string replacement in order to replace XXX_XXX with your desired date, e.g., Sep_Oct; 3) "write" out the file as Text and close it; 4) "Include" the new text file to run it. Each month, you change the desired date, and then run the Report. -- Rich Ulrich Date: Mon, 19 Dec 2011 12:57:25 -0800 From: [hidden email] Subject: macro to replace portions of variable names in syntax file To: [hidden email] All, I like being able to update the dates on my files using this simple macro: DEFINE !filedate() '20111219' !ENDDEFINE. But now I have variable names that have a date embedded in them. I want to update all instances of those variable names with something like a macro. For example, a typical command might be: if (Sep_Oct_2011_credits = 1) priordebitmatched_Sep_Oct_2011_credits_credits = 1. I want to change the month and date portions since they will change every time I run the file, and I don’t want to have to convert all the syntax to a macro. That seems overkill. [...] |
Administrator
|
In reply to this post by mpirritano
I believe you shall have grave difficulties resolving something of the form
'20111219' to anything resembling Sep_Oct_2011 (or probably more likely Nov_Dec_2011) aside from serious (complex) parsing within said MACRO or Python program. May I suggest isolation of date dependent logic in a separate file as a macro (or python program if you prefer) and INSERT or INCLUDE it. A more complete example of your date dependent code would be useful for eliciting more useful suggestions on further code development.
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 mpirritano
Hi Matthew,
You could spss.setMacroValue to assign the new macro value to a macro name. That way you don't need a big program block. Maybe something like this untested code will do the trick:
import tkSimpleDialog
import spss result = tkSimpleDialog.askstring("Date variable", "Enter date variable name") spss.SetMacroValue("!varname", result) You just use the !varname macro whereever you need it in the rest of your syntax.
Cheers!!
Albert-Jan ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
In reply to this post by David Marso
David et al.,
Apologies if my original post wasn't clear. I think Jon and Albert-Jan were on the right track. I have yet to try Albert-Jan's solution. What I want is simply a constant. Like in python I could just say: X = "Hello" And then plug that in wherever else I want in the syntax. To be able to use a constant that is not expressly a string, like Sep_Oct_2011 (no quotes), as part of a variable name. So that I could just say X = Sep_Oct_2011 and then plug X in wherever I wanted to do the replacement. That is what I was using the macro syntax to define my file dates as. I don't need to parse the date string to equal anything. 20111219 doesn't have to equal Nov_Dec_2011. In fact it was silly of me to call my variable MMM_MMM_YYYY. I changed it from Sep_Oct_2011 to 090111_103111, for my sep to oct file name. I prefer YYYYMMDD, but the person who is sending me files uses MMDDYY. Thanks! Matt Matthew Pirritano, Ph.D. Research Analyst IV Medical Services Initiative (MSI) Orange County Health Care Agency (714) 568-5648 -----Original Message----- From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of David Marso Sent: Monday, December 19, 2011 9:19 PM To: [hidden email] Subject: Re: macro to replace portions of variable names in syntax file I believe you shall have grave difficulties resolving something of the form '20111219' to anything resembling Sep_Oct_2011 (or probably more likely Nov_Dec_2011) aside from serious (complex) parsing within said MACRO or Python program. May I suggest isolation of date dependent logic in a separate file as a macro (or python program if you prefer) and INSERT or INCLUDE it. A more complete example of your date dependent code would be useful for eliciting more useful suggestions on further code development. mpirritano wrote > > All, > > > > I like being able to update the dates on my files using this simple > macro: > > > > DEFINE !filedate() '20111219' !ENDDEFINE. > > > > But now I have variable names that have a date embedded in them. I > to update all instances of those variable names with something like a > macro. > > > > For example, a typical command might be: > > > > if (Sep_Oct_2011_credits = 1) > priordebitmatched_Sep_Oct_2011_credits_credits = 1. > > > > I want to change the month and date portions since they will change > every time I run the file, and I don't want to have to convert all the > syntax to a macro. That seems overkill. > > > > Thanks > > Matt > > > > > > > > Matthew Pirritano, Ph.D. > > Research Analyst IV > > Medical Services Initiative (MSI) > > Orange County Health Care Agency > > (714) 568-5648 > -- View this message in context: http://spssx-discussion.1045642.n5.nabble.com/macro-to-replace-portions- of-variable-names-in-syntax-file-tp5087267p5087990.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 |
Read a template, substitute for some text, write the result somewhere
and then run it. That's the way that I read the original post. My old-style solution still looks pretty easy, compared to what I've read, which mostly tackle other problems. The text substitution can be more complicated than I wrote -- when the new and old are not exactly the same length, you have to copy the old line into a new line before writing it, to accommodate a longer or shorter text. -- Rich Ulrich > Date: Tue, 20 Dec 2011 08:31:23 -0800 > From: [hidden email] > Subject: Re: macro to replace portions of variable names in syntax file > To: [hidden email] > > David et al., > > Apologies if my original post wasn't clear. I think Jon and Albert-Jan > were on the right track. I have yet to try Albert-Jan's solution. > > What I want is simply a constant. > > Like in python I could just say: > > X = "Hello" > > And then plug that in wherever else I want in the syntax. To be able to > use a constant that is not expressly a string, like Sep_Oct_2011 (no > quotes), as part of a variable name. So that I could just say X = > Sep_Oct_2011 and then plug X in wherever I wanted to do the replacement. > That is what I was using the macro syntax to define my file dates as. I > don't need to parse the date string to equal anything. 20111219 doesn't > have to equal Nov_Dec_2011. In fact it was silly of me to call my > variable MMM_MMM_YYYY. I changed it from Sep_Oct_2011 to 090111_103111, > for my sep to oct file name. I prefer YYYYMMDD, but the person who is > sending me files uses MMDDYY. > |
Thanks Rich, I’m not sure I’m clear on what
people are calling a template. Is there an actual spss syntax file custom template?
Or do you just mean a template in the generic sense of a preset file format? Or
is it something else? Are you talking about doing text
substitution using python? Or are you talking about an old-style spss macro? Or
doing find-replace? I guess I wasn’t too clear on the
procedure you were describing. If it’s not too much to ask, could you
provide a little more detail? Thanks Matt Matthew Pirritano, Ph.D. Research Analyst IV Medical Services Initiative (MSI) Orange County Health Care Agency (714) 568-5648 From: Rich Ulrich
[mailto:[hidden email]] Read a template, substitute for
some text, write the result somewhere > Date: Tue, 20 Dec 2011 08:31:23 -0800 |
Administrator
|
define !const() 12_13_2011 !ENDDEFINE .
DEFINE !DATEMO (). compute !CONCAT(xxxx,!EVAL(!const),yyy)=!CONCAT(xxxx,!EVAL(!const),yyy,'_copy'). !ENDDEFINE . SET MPRINT ON. !DATEMO . 103 0 M> 104 0 M> . 105 0 M> COMPUTE XXXX12_13_2011YYY = XXXX12_13_2011YYY_copy 106 0 M> . list.
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 mpirritano
I don't have the commands on hand, but see below, for more detail.
Date: Tue, 20 Dec 2011 11:18:13 -0800 From: [hidden email] Subject: Re: macro to replace portions of variable names in syntax file To: [hidden email] Thanks Rich,
"I’m not sure I’m clear on what people are calling a template. Is there an actual spss syntax file custom template? Or do you just mean a template in the generic sense of a preset file format? Or is it something else?"
"Template" is a generic idea. For what I proposed, you *could* use as a template exactly the same thing.sps syntax file that already ran. I suggest that this "template" should be written with a dummy like XXXXXXXX (with the proper number of characters) in the place of what will be the date-related text. "Are you talking about doing text substitution using python? Or are you talking about an old-style spss macro? Or doing find-replace? I guess I wasn’t too clear on the procedure you were describing. If it’s not too much to ask, could you provide a little more detail?"
[snip, previous]
I'm talking about using SPSS - no Python, no macro - to do what is *essentially* find-replace. You read the template thing.sps and write out a modified version, to_do.sps -- which will be run later. You specify "thing.sps" as the input file, with on variable, Line, that is an A80 (max line?) variable. You could use Index( ...) a couple of times to find where that dummy starts, and Substring replacement to plug in the actual Text for this run -- This is the one line that would be changed for a new run. Or, I think there is a generic "substitute" command which is handier, but I've never had the need for it. Then you use WRITE, not SAVE, to write out the modified Line. (At this point, I think you need something to Close the file.) Then you use "Include file= to_do.sps" -- and that brings in the newly written file, to be run. Afterwords, you have the to_do.sps on disk as documentation that you ran the job; or you can delete it. I don't have SPSS available so I can't check up on these commands. -- Rich Ulrich |
Hi, |
Administrator
|
In reply to this post by Rich Ulrich
GACK!!! CHOKE!!!
Why not just write a bloody MACRO for crying out loud. This whole "template" idea people are throwing around is a giant step backwards! By the time you have such functional template it will be one hell of a LOT uglier than any MACRO. --- *"You specify "thing.sps" as the input file, with on variable, Line, that is an A80 (max line?) variable." AFAIK there is *NO* 80 line MAX for files to be "INCLUDED" unless they contain inline data. From the FM: "• Commands in an included file must begin in column 1, and continuation lines for each command must be indented at least one column. • A raw data file can be used as an include file if the first line of the included file contains the BEGIN DATA command and the last line contains the END DATA command. However, because the data are specified between BEGIN DATA and END DATA, they are limited to a maximum of 80 columns (the maximum may be fewer than 80 columns on some systems). • As many INCLUDE commands as needed can be used in a session. • INCLUDE commands can be nested so that one set of included commands includes an- other set of commands. This nesting can go to five levels. However, a file cannot be included that is still open from a previous step." NB: This documentation is from version 11.5, things may be different in more recent versions *"You could use Index( ...) a couple of times to find where that dummy starts, and Substring replacement to plug in the actual Text for this run -- This is the one line that would be changed for a new run. Or, I think there is a generic "substitute" command which is handier, but I've never had the need for it." I believe the function is REPLACE (available in more recent versions). *"Then you use WRITE, not SAVE, to write out the modified Line. (At this point, I think you need something to Close the file.)". One would use EXECUTE to pass the data to "WRITE" the file and close the handle. *"Then you use "Include file= to_do.sps" -- and that brings in the newly written file, to be run." People these data recommend using INSERT rather than INCLUDE as it has less stringent requirements. <SNIP>. *BUT*!!!! I still DON'T recommend this approach!!! Do it in MACRO or Python for crying out loud!
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?" |
Seasons Greetings! I am currently on leave until the 17th January and will not be looking at emails during this time. If it is urgent, please email me on the 17th January.
|
Free forum by Nabble | Edit this page |