Trouble with DEFINE marco

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

Trouble with DEFINE marco

Little, Jonathon
Dear LISTERS,
 
I have a variable, TIME_STAMP, which is in a temporary data file.  
 
I want make the value for TIME_STAMP  equal to the global macro
!Census_date, so that I concatenate !census_date into various table
titles when running tables on  other data files.  Also TIME_STAMP
includes day, month,year, hour,minute,second while !census_date needs to
be in the day,month,year format.
 
I need only to know hoe to define the !census_date to equal the value
held in TIME_STAMP.
 
Any assistance would be greatly appreciated.

DEFINE !Census_Date () __________________ !ENDDEFINE.

Kind regards,

Jonathon

 

====================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: Trouble with DEFINE marco

Richard Ristow
At 12:32 AM 2/12/2008, Little, Jonathon wrote:

>I have a variable, TIME_STAMP, which is in a temporary data file.
>
>I want make the value for TIME_STAMP  equal to the global macro !Census_date,
>Also TIME_STAMP includes day, month,year, hour,minute,second while
>!census_date needs to be in the day,month,year format.
>
>I need to know how to define the !census_date to equal the value
>held in TIME_STAMP.
>
>DEFINE !Census_Date () __________________ !ENDDEFINE.

You can't do it in direct syntax like that. The classic method is to
generate the DEFINE code as an SPSS string value, write it to an
external file, and INSERT it back in your code.

The following is not tested. (For tested code for a similar but not
identical problem, see the APPENDIX to this posting.)

. This code requires that 'GCode' be a filename or file handle (*not*
dataset name) that may be used as a scratch file.
. You don't say how variable 'TIME_STAMP' is represented. The
following assumes it's an SPSS date, or date-time, variable, and
produces a value in form "mm/dd/yyyy". Modify as needed. (See
statement "COMPUTE Dte_TEXT...)
. The "PRINT" statement displays the generated code, for debugging
and verification. It is not necessary for operation.
. The value of 'TIME_STAMP' is taken from the first case in the active file.

STRING MAC_TEXT(A85).
STRING Dte_Text(A10).
DO IF $CASENUM EQ 1.
.  COMPUTE Dte_TEXT =STRING(TIME_STAMP,ADATE10).
.  COMPUTE MAC_TEXT =CONCAT("define !Census_Date () "
                            ,DteTEXT
                            ," !enddefine.").
.  PRINT / 'Macro text is:'
          / MAC_TEXT.
.  WRITE OUTFILE=GCode
          / MAC_TEXT.
EXECUTE.

INSERT GCOde.

.........
APPENDIX: From
Date:    Wed, 19 Jul 2006 00:52:38 -0400
From:    Richard Ristow <[hidden email]>
Subject: Re: Saving Today's Date as Part of Filename?
To:      [hidden email]

>I'm having no luck figuring out how to have the current date
>included as part of the save out filename. It would be great to be
>able to do something like this:
>
>Save out = 'c:\temp\filename_' + (TODAY'S DATE) + '.sav'.

It creates a macro !act that evaluates to

'c:\TempStor\SPSS\dd-MMM-yyyy hh-mm.sav'

It's tested code; this is SPSS draft output. There's some code that's
in just to trace what's happening, so it could be shortened.
..........................
*  Create the macro definition, and write it to a file           .
INPUT PROGRAM.
.   STRING  TIME_NOW (A17).
.   COMPUTE TIME_NOW = STRING($TIME,DATETIME17).
.   END CASE.
END FILE.
END INPUT PROGRAM.
/* Change colon in time value to a hyphen                        .
COMPUTE #WHERE =     INDEX(TIME_NOW,':').
COMPUTE SUBSTR(TIME_NOW,#WHERE,1) = '-'.
STRING MAC_TEXT(A85).
COMPUTE MAC_TEXT =CONCAT("define !act ()"
                         ,"'"
                            ,"c:\TempStor\SPSS\",TIME_NOW,".sav"
                         ,"' "
                         ,"!enddefine.").
PRINT / 'Macro text is:'
       / MAC_TEXT.
WRITE OUTFILE='c:\TempStor\SPSS\2004-04-01 Moser.Mac'
       / MAC_TEXT.
EXECUTE.
Macro text is:
define !act ()'c:\TempStor\SPSS\01-APR-2004 14-15.sav' !enddefine.
NEW FILE.

*  Recover the macro definition, define the macro, and display  .
INCLUDE 'c:\TempStor\SPSS\2004-04-01 Moser.Mac'.
   52  0  define !act ()'c:\TempStor\SPSS\01-APR-2004 14-15.sav'
!enddefine.
   55  0
   57  0  * End of INCLUDE nesting level 01.

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