Transform a numeric macro body into other numbers

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

Transform a numeric macro body into other numbers

ronald1
Hi all,

Hope someone can help me with the following question. It seems easy, but…

 I am currently programming a syntax in which I need to read (GET) and SAVE a lot of datasets from different years and months, and every month I need to run the analyses again. My question: If I specify the body of a macro as a year (number), can I easily transform this macro number into other numbers? For example, in the beginning of my syntax file I specify a year, let’s say 2011. I use it to GET data from, for instance, path2011.sav. However, at the same time, I also want to get data from 2010 and 2012 elsewhere in the syntax, as well as from other years, e.g. 2008.

At the start of my syntax I specify:

DEFINE !jaar () ‘2011’
!ENDDEFINE.

It is possible, as I tried, to automatically transform ‘2011’ in ‘2012’ using the following:

DEFINE !jaarPLUS1 ()
!LET !jaarPLUS1=+!jaar+1
!ENDDEFINE.

However, -1 doesn’t work here to get ´2010´ (or -3 to get ´2008´). Does anyone know how I can easily achieve this?

The same for the following problem: At the beginning of my syntax I specify a number: DEFINE !Q () ´1´ !ENDDEFINE. The same for 2,3 or 4. If !Q is a ´1´ I want to use it to GET and SAVE file names with a 11,17, and 19 in it (e.g. path11.sav, path17.sav, path19.sav). If it is a 2, I later want to use it to GET and SAVE file names with a 14,15, and 18, etc.  

Is there a way to transform a macro (as below) into three macros which take the values of 11, 17 and 19?

DEFINE !Q () ‘1’
!ENDDEFINE.

Hope someone can help me out! Thanks a lot!

Ronald
Reply | Threaded
Open this post in threaded view
|

Automatic reply: Transform a numeric macro body into other numbers

Valerie Villella
Thank you for your email.

I am on vacation until Monday August 27 and will not be accessing my emails during this time. I will respond to your message upon my return to the office.

Have a wonderful day.

Valerie Villella
Education Coordinator &
Policy and Program Analyst

=====================
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: Transform a numeric macro body into other numbers

David Marso
Administrator
In reply to this post by ronald1
Bottom line is that MACRO is utterly brain dead WRT mathematical operations (+,-,*...).
You can hack away using !CONCAT() and !LENGTH () but it is a pretty sorry solution.
It is also easy to create an ugly mess and get lost in the slimy details.
Here is a hackadelic approach to Plus1.  I leave it to you to work out subtraction.
In general defining some constant as a macro at the top of your syntax is a sad approach!
You should really learn how to pass arguments (per this example).
In general if you are just beginning with MACRO (as is obvious) you would do yourself a huge favor by jumping into Python or Basic Scripting and leave taming the MACRO beast to us recalcitrant old farts.
---

DEFINE !jaarPLUS1 ( Jarr !TOKENS(1))
!LET !J12=!SUBSTR(!EVAL(!Jarr),1,2).
!LET !J32=!SUBSTR(!EVAL(!Jarr),3,2) .
!LET !L="x" !DO !I = 1 !TO !J32 !LET !L=!CONCAT(!L,"x") !DOEND
!LET !jaarPLUS=!LENGTH(!L)
GET FILE !QUOTE(!CONCAT(!J12,!Jaarplus) ).

!ENDDEFINE.
DATA LIST FREE / x.
BEGIN DATA
1
END DATA.
SET MPRINT ON PRINTBACK ON.

 !jaarplus1 Jarr=2012 .
Results in
 GET FILE '2013'

ronald1 wrote
Hi all,

Hope someone can help me with the following question. It seems easy, but…

 I am currently programming a syntax in which I need to read (GET) and SAVE a lot of datasets from different years and months, and every month I need to run the analyses again. My question: If I specify the body of a macro as a year (number), can I easily transform this macro number into other numbers? For example, in the beginning of my syntax file I specify a year, let’s say 2011. I use it to GET data from, for instance, path2011.sav. However, at the same time, I also want to get data from 2010 and 2012 elsewhere in the syntax, as well as from other years, e.g. 2008.

At the start of my syntax I specify:

DEFINE !jaar () ‘2011’
!ENDDEFINE.

It is possible, as I tried, to automatically transform ‘2011’ in ‘2012’ using the following:

DEFINE !jaarPLUS1 ()
!LET !jaarPLUS1=+!jaar+1
!ENDDEFINE.

However, -1 doesn’t work here to get ´2010´ (or -3 to get ´2008´). Does anyone know how I can easily achieve this?

The same for the following problem: At the beginning of my syntax I specify a number: DEFINE !Q () ´1´ !ENDDEFINE. The same for 2,3 or 4. If !Q is a ´1´ I want to use it to GET and SAVE file names with a 11,17, and 19 in it (e.g. path11.sav, path17.sav, path19.sav). If it is a 2, I later want to use it to GET and SAVE file names with a 14,15, and 18, etc.  

Is there a way to transform a macro (as below) into three macros which take the values of 11, 17 and 19?

DEFINE !Q () ‘1’
!ENDDEFINE.

Hope someone can help me out! Thanks a lot!

Ronald
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?"
Reply | Threaded
Open this post in threaded view
|

Re: Transform a numeric macro body into other numbers

David Marso
Administrator
OK WTF: Subtraction ;-)

define !subtract (valx= !Tokens(1) / minus !tokens(1) ).

   !LET !X= "X" !DO !I=1 !TO !minus  !LET !X=!CONCAT(!X,"X") !DOEND
   !LET !Y=!NULL  !DO !I=!LENGTH(!X)  !TO !ValX !LET !Y=!CONCAT(!Y,"X")   !DOEND
   !LET !Z=!LENGTH(!Y)
   GET FILE !QUOTE(!Z).
!enddefine.

SET MPRINT ON PRINTBACK ON.

!subtract Valx=10 Minus=3 .

David Marso wrote
Bottom line is that MACRO is utterly brain dead WRT mathematical operations (+,-,*...).
You can hack away using !CONCAT() and !LENGTH () but it is a pretty sorry solution.
It is also easy to create an ugly mess and get lost in the slimy details.
Here is a hackadelic approach to Plus1.  I leave it to you to work out subtraction.
In general defining some constant as a macro at the top of your syntax is a sad approach!
You should really learn how to pass arguments (per this example).
In general if you are just beginning with MACRO (as is obvious) you would do yourself a huge favor by jumping into Python or Basic Scripting and leave taming the MACRO beast to us recalcitrant old farts.
---

DEFINE !jaarPLUS1 ( Jarr !TOKENS(1))
!LET !J12=!SUBSTR(!EVAL(!Jarr),1,2).
!LET !J32=!SUBSTR(!EVAL(!Jarr),3,2) .
!LET !L="x" !DO !I = 1 !TO !J32 !LET !L=!CONCAT(!L,"x") !DOEND
!LET !jaarPLUS=!LENGTH(!L)
GET FILE !QUOTE(!CONCAT(!J12,!Jaarplus) ).

!ENDDEFINE.
DATA LIST FREE / x.
BEGIN DATA
1
END DATA.
SET MPRINT ON PRINTBACK ON.

 !jaarplus1 Jarr=2012 .
Results in
 GET FILE '2013'

ronald1 wrote
Hi all,

Hope someone can help me with the following question. It seems easy, but…

 I am currently programming a syntax in which I need to read (GET) and SAVE a lot of datasets from different years and months, and every month I need to run the analyses again. My question: If I specify the body of a macro as a year (number), can I easily transform this macro number into other numbers? For example, in the beginning of my syntax file I specify a year, let’s say 2011. I use it to GET data from, for instance, path2011.sav. However, at the same time, I also want to get data from 2010 and 2012 elsewhere in the syntax, as well as from other years, e.g. 2008.

At the start of my syntax I specify:

DEFINE !jaar () ‘2011’
!ENDDEFINE.

It is possible, as I tried, to automatically transform ‘2011’ in ‘2012’ using the following:

DEFINE !jaarPLUS1 ()
!LET !jaarPLUS1=+!jaar+1
!ENDDEFINE.

However, -1 doesn’t work here to get ´2010´ (or -3 to get ´2008´). Does anyone know how I can easily achieve this?

The same for the following problem: At the beginning of my syntax I specify a number: DEFINE !Q () ´1´ !ENDDEFINE. The same for 2,3 or 4. If !Q is a ´1´ I want to use it to GET and SAVE file names with a 11,17, and 19 in it (e.g. path11.sav, path17.sav, path19.sav). If it is a 2, I later want to use it to GET and SAVE file names with a 14,15, and 18, etc.  

Is there a way to transform a macro (as below) into three macros which take the values of 11, 17 and 19?

DEFINE !Q () ‘1’
!ENDDEFINE.

Hope someone can help me out! Thanks a lot!

Ronald
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?"
Reply | Threaded
Open this post in threaded view
|

Re: Transform a numeric macro body into other numbers

ronald1
Great, thanks a lot for your reply and advice David! I am sure this will help. Indeed, I am just beginning with macros. Anyway, I hope to learn the macro language fast.

If I understand you correctly, it would be even more problematic to change a number - rather than substracting or adding a certain value - in some (more random) "other numbers".

Specifically, I also included a macro which can either take a value of '1','2','3', or '4'; e.g., DEFINE !NR () '1' !ENDDEFINE. If it is a '1' I need to get data from multiple files with 3 different numbers (these are 3 other numbers when it is a 2,3 or 4), and more than 15 different paths (e.g. pathX'11', pathY'11', pathZ'11', pathX'28', pathY'28', pathX'1727', pathY'1727', etc....). It would be ideal if I could GET data by pathX'!newNR1' for 11, pathX'!newNR2' for 28 etc.  

Do you think this is possible, or would there be only a sorry solution again :) ?
Reply | Threaded
Open this post in threaded view
|

Re: Transform a numeric macro body into other numbers

David Marso
Administrator
You really need to clarify the question.
Maybe present a table of the path/number combinations?
As I previously stated.  Setting up macro constants is a feeble approach.  
Learn how to pass parameters to macros (there are several options).
You will also want to study the !DO operator and list processing along with the !CONCAT, !QUOTE and !UNQUOTE operators.  Meanwhile, since you are just beginning to defile your mind with macros you might find your time better invested in acquiring python or Basic scripting skills (to say nothing of costs associated with imminent hair loss, intervention by mental health professionals and replacing smashed keyboards) .

DEFINE blah (path !TOKENS(1) / flist !ENCLOSE ("[","]") )
!DO !F !IN (!flist )
GET FILE !QUOTE(!CONCAT(!UNQUOTE(!path),!F)).
!DOEND
!ENDDEFINE.

SET MPRINT ON PRINTBACK ON.
blah path 'C:\temp\' flist [1 2 4 6 7].

blah path 'C:\temp\' flist [1 2 4 6 7].
Results in:

GET FILE 'C:\temp\1'.
GET FILE 'C:\temp\2'.
GET FILE 'C:\temp\4'.
GET FILE 'C:\temp\6'.
GET FILE 'C:\temp\7'.

ronald1 wrote
Great, thanks a lot for your reply and advice David! I am sure this will help. Indeed, I am just beginning with macros. Anyway, I hope to learn the macro language fast.

If I understand you correctly, it would be even more problematic to change a number - rather than substracting or adding a certain value - in some (more random) "other numbers".

Specifically, I also included a macro which can either take a value of '1','2','3', or '4'; e.g., DEFINE !NR () '1' !ENDDEFINE. If it is a '1' I need to get data from multiple files with 3 different numbers (these are 3 other numbers when it is a 2,3 or 4), and more than 15 different paths (e.g. pathX'11', pathY'11', pathZ'11', pathX'28', pathY'28', pathX'1727', pathY'1727', etc....). It would be ideal if I could GET data by pathX'!newNR1' for 11, pathX'!newNR2' for 28 etc.  

Do you think this is possible, or would there be only a sorry solution again :) ?
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?"