Looping Through A Data Set ?

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

Looping Through A Data Set ?

SUBSCRIBE SPSSX-JohnF
Any input will be appreciated.  I have a source file of 5 years of data, 2015, 2016...2019.  Currently, I use this simple macro:

define YYYY () 2015.  
!enddefine.
define YYstr () "15"
!enddefine.

and the code which follows this macro combines 2015, 2016 and 2017 data, runs transformations and creates\saves out an SPSS data set.  The issue is, then I need to change 2015 to 2016 so the code can run again.  I do this through 2019.  Ultimately this results in 5 separate data sets.  Lastly I want to append the 5 data sets together and I have existing code which does that but I'd like to avoid entering the years over and over., but I am at a loss as to how and do this....Sincerely and Thanks for any assistance.

=====================
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: Looping Through A Data Set ?

spss.giesel@yahoo.de
Hi!

DEFINE YYYY (start = !TOKENS(1) / finish = !TOKENS(1)) 
!DO !y = start !TO finish 
<your statements>
!DOEND
!ENDDEFINE.

YYYY start = 2015 finish = 2019.

DEFINE YYstr (start = !TOKENS(1) / finish = !TOKENS(1)) 
!DO !y = start !TO finish 
!LET !mystring = !QUOTE(!y)
<your statements>
!DOEND
!ENDDEFINE.

YYYY start = 15 finish = 19.


Mario Giesel
Munich, Germany


Am Dienstag, 7. April 2020, 17:43:32 MESZ hat SUBSCRIBE SPSSX-JohnF <[hidden email]> Folgendes geschrieben:


Any input will be appreciated.  I have a source file of 5 years of data, 2015, 2016...2019.  Currently, I use this simple macro:

define YYYY () 2015. 
!enddefine.
define YYstr () "15"
!enddefine.

and the code which follows this macro combines 2015, 2016 and 2017 data, runs transformations and creates\saves out an SPSS data set.  The issue is, then I need to change 2015 to 2016 so the code can run again.  I do this through 2019.  Ultimately this results in 5 separate data sets.  Lastly I want to append the 5 data sets together and I have existing code which does that but I'd like to avoid entering the years over and over., but I am at a loss as to how and do this....Sincerely and Thanks for any assistance.

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

Re: Looping Through A Data Set ?

spss.giesel@yahoo.de
Hi!

DEFINE YYYY (start = !TOKENS(1) / finish = !TOKENS(1)) 
!DO !y = start !TO finish 
<your statements>
!DOEND
!ENDDEFINE.

YYYY start = 2015 finish = 2019.

DEFINE YYstr (start = !TOKENS(1) / finish = !TOKENS(1)) 
!DO !y = start !TO finish 
!LET !mystring = !QUOTE(!y)
<your statements>
!DOEND
!ENDDEFINE.

YYYY start = 15 finish = 19.


Mario Giesel
Munich, Germany


Am Dienstag, 7. April 2020, 19:23:51 MESZ hat Mario Giesel <[hidden email]> Folgendes geschrieben:


Hi!

DEFINE YYYY (start = !TOKENS(1) / finish = !TOKENS(1)) 
!DO !y = start !TO finish 
<your statements>
!DOEND
!ENDDEFINE.

YYYY start = 2015 finish = 2019.

DEFINE YYstr (start = !TOKENS(1) / finish = !TOKENS(1)) 
!DO !y = start !TO finish 
!LET !mystring = !QUOTE(!y)
<your statements>
!DOEND
!ENDDEFINE.

YYYY start = 15 finish = 19.


Mario Giesel
Munich, Germany


Am Dienstag, 7. April 2020, 17:43:32 MESZ hat SUBSCRIBE SPSSX-JohnF <[hidden email]> Folgendes geschrieben:


Any input will be appreciated.  I have a source file of 5 years of data, 2015, 2016...2019.  Currently, I use this simple macro:

define YYYY () 2015. 
!enddefine.
define YYstr () "15"
!enddefine.

and the code which follows this macro combines 2015, 2016 and 2017 data, runs transformations and creates\saves out an SPSS data set.  The issue is, then I need to change 2015 to 2016 so the code can run again.  I do this through 2019.  Ultimately this results in 5 separate data sets.  Lastly I want to append the 5 data sets together and I have existing code which does that but I'd like to avoid entering the years over and over., but I am at a loss as to how and do this....Sincerely and Thanks for any assistance.

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

Re: Looping Through A Data Set ?

spss.giesel@yahoo.de
Sorry, some exclamations missing ...

DEFINE YYYY (start = !TOKENS(1) / finish = !TOKENS(1)) 
!DO !y = !start !TO !finish 
<your statements>
!DOEND
!ENDDEFINE.

YYYY start = 2015 finish = 2019.

DEFINE YYstr (start = !TOKENS(1) / finish = !TOKENS(1)) 
!DO !y = !start !TO !finish 
!LET !mystring = !QUOTE(!y)
<your statements>
!DOEND
!ENDDEFINE.

YYYY start = 15 finish = 19.


Mario Giesel
Munich, Germany




Am Dienstag, 7. April 2020, 19:25:29 MESZ hat Mario Giesel <[hidden email]> Folgendes geschrieben:


Hi!

DEFINE YYYY (start = !TOKENS(1) / finish = !TOKENS(1)) 
!DO !y = start !TO finish 
<your statements>
!DOEND
!ENDDEFINE.

YYYY start = 2015 finish = 2019.

DEFINE YYstr (start = !TOKENS(1) / finish = !TOKENS(1)) 
!DO !y = start !TO finish 
!LET !mystring = !QUOTE(!y)
<your statements>
!DOEND
!ENDDEFINE.

YYYY start = 15 finish = 19.


Mario Giesel
Munich, Germany


Am Dienstag, 7. April 2020, 19:23:51 MESZ hat Mario Giesel <[hidden email]> Folgendes geschrieben:


Hi!

DEFINE YYYY (start = !TOKENS(1) / finish = !TOKENS(1)) 
!DO !y = start !TO finish 
<your statements>
!DOEND
!ENDDEFINE.

YYYY start = 2015 finish = 2019.

DEFINE YYstr (start = !TOKENS(1) / finish = !TOKENS(1)) 
!DO !y = start !TO finish 
!LET !mystring = !QUOTE(!y)
<your statements>
!DOEND
!ENDDEFINE.

YYYY start = 15 finish = 19.


Mario Giesel
Munich, Germany


Am Dienstag, 7. April 2020, 17:43:32 MESZ hat SUBSCRIBE SPSSX-JohnF <[hidden email]> Folgendes geschrieben:


Any input will be appreciated.  I have a source file of 5 years of data, 2015, 2016...2019.  Currently, I use this simple macro:

define YYYY () 2015. 
!enddefine.
define YYstr () "15"
!enddefine.

and the code which follows this macro combines 2015, 2016 and 2017 data, runs transformations and creates\saves out an SPSS data set.  The issue is, then I need to change 2015 to 2016 so the code can run again.  I do this through 2019.  Ultimately this results in 5 separate data sets.  Lastly I want to append the 5 data sets together and I have existing code which does that but I'd like to avoid entering the years over and over., but I am at a loss as to how and do this....Sincerely and Thanks for any assistance.

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

Re: Looping Through A Data Set ?

SUBSCRIBE SPSSX-JohnF
In reply to this post by SUBSCRIBE SPSSX-JohnF
Thank you but unfortunately I don't understand the use of the macro(s).

Using the years 2014,2015,2016....2019

In the body of my existing code I have statements like

compute PeriodEnd=yrmoda(YYYY,09,30).

and in the end I have save outfile statement like:

save outfile='C:\Context_Data\PermTotal\TotalPerm_'+ PeriodType + YYstr + '.sav'.

and currently, after each running of the existing code in my simple macro's

define YYYY () 2014.  
!enddefine.
define YYstr () "14"
!enddefine.

I need to change 2014 to 2015, 2015 to 2016 etc. My guess the presented macro will do the trick
but my knowledge around macro's is too limited to get it.    



=====================
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: Looping Through A Data Set ?

spss.giesel@yahoo.de
Difficult to advice without complete code.
Can you share a working example with example data?


Mario Giesel
Munich, Germany


Am Mittwoch, 8. April 2020, 20:06:06 MESZ hat SUBSCRIBE SPSSX-JohnF <[hidden email]> Folgendes geschrieben:


Thank you but unfortunately I don't understand the use of the macro(s).

Using the years 2014,2015,2016....2019

In the body of my existing code I have statements like

compute PeriodEnd=yrmoda(YYYY,09,30).

and in the end I have save outfile statement like:

save outfile='C:\Context_Data\PermTotal\TotalPerm_'+ PeriodType + YYstr + '.sav'.

and currently, after each running of the existing code in my simple macro's

define YYYY () 2014. 
!enddefine.
define YYstr () "14"
!enddefine.

I need to change 2014 to 2015, 2015 to 2016 etc. My guess the presented macro will do the trick
but my knowledge around macro's is too limited to get it.   



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

Re: Looping Through A Data Set ?

SUBSCRIBE SPSSX-JohnF
In reply to this post by SUBSCRIBE SPSSX-JohnF
I hope this additional code helps....Thanks for checking in.

*************************************************

define PeriodType () "AB"
!enddefine.
define YYYY () 2015.  
!enddefine.
define YYstr () "15"
!enddefine.

get file 'C:\Source data.sav'.
dataset name Indicator window=front.

do if PeriodType = "AB".
compute DtPeriodBeg=date.mdy(10,01,YYYY-1).
compute DtPeriodEnd=date.mdy(09,30,YYYY).
compute DtMeasureEnd=date.mdy(09,30,YYYY+3).
compute DtMeasureEnd_Minus6Mo=date.mdy(03,31,YYYY+3).
end if.
do if PeriodType = "BA".
compute DtPeriodBeg=date.mdy(04,01,YYYY-1).
compute DtPeriodEnd=date.mdy(03,31,YYYY).
compute DtMeasureEnd=date.mdy(03,31,YYYY+3).
compute DtMeasureEnd_Minus6Mo=date.mdy(09,30,YYYY+2).
end if.
execute.
formats DtPeriodBeg DtPeriodEnd DtMeasureEnd DtMeasureEnd_Minus6Mo (adate10).

if (DtReportBeg ge DtPeriodBeg) and (DtReportEnd le DtMeasureEnd) ReportedDuringPeriod = 1.
execute.

select if ReportedDuringPeriod=1.
execute.

delete variables ReportedDuringPeriod.

string TwelveMoCohort (A4).
compute TwelveMoCohort = concat(PeriodType,YYstr).
execute.

save outfile='C:\Data\Total_'+ PeriodType + YYstr + '.sav'.
exe.

=====================
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: Looping Through A Data Set ?

spss.giesel@yahoo.de
There's an ambiguity in your code when you say:
define PeriodType () "AB" !enddefine.

and later you say:
do if PeriodType = "AB".

and then you say:
compute TwelveMoCohort = concat(PeriodType,YYstr).

I guess the latter two commands refer to a variable in your database.
If this is the case (otherwise your code wouldn't make sense) you can probably use this macro (which is untested, of course):

***** MACRO START.
DEFINE !dostuff(PeriodType = !CHAREND('/')
/Ystart = !TOKENS(1)
/Yfinish = !TOKENS(1))
PRESERVE.
SET PRINTBACK = ON MPRINT = ON /* for debugging purposes */.
get file 'C:\Source data.sav'.
dataset name Indicator window=front.

!DO !y = !Ystart !TO !Yfinish
- do if (PeriodType = "AB").
- compute DtPeriodBeg=date.mdy(10,01,!y-1).
- compute DtPeriodEnd=date.mdy(09,30,!y).
- compute DtMeasureEnd=date.mdy(09,30,!y+3).
- compute DtMeasureEnd_Minus6Mo=date.mdy(03,31,!y+3).
- end if.

- do if (PeriodType = "BA").
- compute DtPeriodBeg=date.mdy(04,01,!y-1).
- compute DtPeriodEnd=date.mdy(03,31,!y).
- compute DtMeasureEnd=date.mdy(03,31,!y+3).
- compute DtMeasureEnd_Minus6Mo=date.mdy(09,30,!y+2).
- end if.

- formats DtPeriodBeg DtPeriodEnd DtMeasureEnd DtMeasureEnd_Minus6Mo (adate10).
- if (DtReportBeg ge DtPeriodBeg) and (DtReportEnd le DtMeasureEnd) ReportedDuringPeriod = 1.
- select if ReportedDuringPeriod=1.
- execute.
- delete variables ReportedDuringPeriod.
- string TwelveMoCohort (A4).
- compute TwelveMoCohort = concat(PeriodType,!SUBSTR(!y,3)).
- execute.
- save outfile=!CONCAT('C:\Data\Total_', !PeriodType, !SUBSTR(!y,3), '.sav').
!DOEND
RESTORE.
!ENDDEFINE.

* Calls.
!dostuff PeriodType = AB / Ystart = 2015 Yfinish = 2019.
!dostuff PeriodType = BA / Ystart = 2015 Yfinish = 2019.
***** MACRO END.



Mario Giesel
Munich, Germany


Am Mittwoch, 8. April 2020, 22:48:12 MESZ hat SUBSCRIBE SPSSX-JohnF <[hidden email]> Folgendes geschrieben:


I hope this additional code helps....Thanks for checking in.

*************************************************

define PeriodType () "AB"
!enddefine.
define YYYY () 2015. 
!enddefine.
define YYstr () "15"
!enddefine.

get file 'C:\Source data.sav'.
dataset name Indicator window=front.

do if PeriodType = "AB".
compute DtPeriodBeg=date.mdy(10,01,YYYY-1).
compute DtPeriodEnd=date.mdy(09,30,YYYY).
compute DtMeasureEnd=date.mdy(09,30,YYYY+3).
compute DtMeasureEnd_Minus6Mo=date.mdy(03,31,YYYY+3).
end if.
do if PeriodType = "BA".
compute DtPeriodBeg=date.mdy(04,01,YYYY-1).
compute DtPeriodEnd=date.mdy(03,31,YYYY).
compute DtMeasureEnd=date.mdy(03,31,YYYY+3).
compute DtMeasureEnd_Minus6Mo=date.mdy(09,30,YYYY+2).
end if.
execute.
formats DtPeriodBeg DtPeriodEnd DtMeasureEnd DtMeasureEnd_Minus6Mo (adate10).

if (DtReportBeg ge DtPeriodBeg) and (DtReportEnd le DtMeasureEnd) ReportedDuringPeriod = 1.
execute.

select if ReportedDuringPeriod=1.
execute.

delete variables ReportedDuringPeriod.

string TwelveMoCohort (A4).
compute TwelveMoCohort = concat(PeriodType,YYstr).
execute.

save outfile='C:\Data\Total_'+ PeriodType + YYstr + '.sav'.
exe.

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

Re: Looping Through A Data Set ?

spss.giesel@yahoo.de
Additional remark:

If it does not work then try 
!PeriodType
instead of 
PeriodType
within the macro.

Mario Giesel
Munich, Germany


Am Donnerstag, 9. April 2020, 04:45:23 MESZ hat Mario Giesel <[hidden email]> Folgendes geschrieben:


There's an ambiguity in your code when you say:
define PeriodType () "AB" !enddefine.

and later you say:
do if PeriodType = "AB".

and then you say:
compute TwelveMoCohort = concat(PeriodType,YYstr).

I guess the latter two commands refer to a variable in your database.
If this is the case (otherwise your code wouldn't make sense) you can probably use this macro (which is untested, of course):

***** MACRO START.
DEFINE !dostuff(PeriodType = !CHAREND('/')
/Ystart = !TOKENS(1)
/Yfinish = !TOKENS(1))
PRESERVE.
SET PRINTBACK = ON MPRINT = ON /* for debugging purposes */.
get file 'C:\Source data.sav'.
dataset name Indicator window=front.

!DO !y = !Ystart !TO !Yfinish
- do if (PeriodType = "AB").
- compute DtPeriodBeg=date.mdy(10,01,!y-1).
- compute DtPeriodEnd=date.mdy(09,30,!y).
- compute DtMeasureEnd=date.mdy(09,30,!y+3).
- compute DtMeasureEnd_Minus6Mo=date.mdy(03,31,!y+3).
- end if.

- do if (PeriodType = "BA").
- compute DtPeriodBeg=date.mdy(04,01,!y-1).
- compute DtPeriodEnd=date.mdy(03,31,!y).
- compute DtMeasureEnd=date.mdy(03,31,!y+3).
- compute DtMeasureEnd_Minus6Mo=date.mdy(09,30,!y+2).
- end if.

- formats DtPeriodBeg DtPeriodEnd DtMeasureEnd DtMeasureEnd_Minus6Mo (adate10).
- if (DtReportBeg ge DtPeriodBeg) and (DtReportEnd le DtMeasureEnd) ReportedDuringPeriod = 1.
- select if ReportedDuringPeriod=1.
- execute.
- delete variables ReportedDuringPeriod.
- string TwelveMoCohort (A4).
- compute TwelveMoCohort = concat(PeriodType,!SUBSTR(!y,3)).
- execute.
- save outfile=!CONCAT('C:\Data\Total_', !PeriodType, !SUBSTR(!y,3), '.sav').
!DOEND
RESTORE.
!ENDDEFINE.

* Calls.
!dostuff PeriodType = AB / Ystart = 2015 Yfinish = 2019.
!dostuff PeriodType = BA / Ystart = 2015 Yfinish = 2019.
***** MACRO END.



Mario Giesel
Munich, Germany


Am Mittwoch, 8. April 2020, 22:48:12 MESZ hat SUBSCRIBE SPSSX-JohnF <[hidden email]> Folgendes geschrieben:


I hope this additional code helps....Thanks for checking in.

*************************************************

define PeriodType () "AB"
!enddefine.
define YYYY () 2015. 
!enddefine.
define YYstr () "15"
!enddefine.

get file 'C:\Source data.sav'.
dataset name Indicator window=front.

do if PeriodType = "AB".
compute DtPeriodBeg=date.mdy(10,01,YYYY-1).
compute DtPeriodEnd=date.mdy(09,30,YYYY).
compute DtMeasureEnd=date.mdy(09,30,YYYY+3).
compute DtMeasureEnd_Minus6Mo=date.mdy(03,31,YYYY+3).
end if.
do if PeriodType = "BA".
compute DtPeriodBeg=date.mdy(04,01,YYYY-1).
compute DtPeriodEnd=date.mdy(03,31,YYYY).
compute DtMeasureEnd=date.mdy(03,31,YYYY+3).
compute DtMeasureEnd_Minus6Mo=date.mdy(09,30,YYYY+2).
end if.
execute.
formats DtPeriodBeg DtPeriodEnd DtMeasureEnd DtMeasureEnd_Minus6Mo (adate10).

if (DtReportBeg ge DtPeriodBeg) and (DtReportEnd le DtMeasureEnd) ReportedDuringPeriod = 1.
execute.

select if ReportedDuringPeriod=1.
execute.

delete variables ReportedDuringPeriod.

string TwelveMoCohort (A4).
compute TwelveMoCohort = concat(PeriodType,YYstr).
execute.

save outfile='C:\Data\Total_'+ PeriodType + YYstr + '.sav'.
exe.

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

Re: Looping Through A Data Set ?

Rich Ulrich
In reply to this post by spss.giesel@yahoo.de
Mario,

I'm curious - I have never used a Macro as a value being
tested with DO IF, but I can imagine that it could work.
I picture the value of the Macro being substituted, then used.
Do you know that it does not work, or is this an assumption?

I've used a Macro as a list of variables for an argument,
so I would be more likely to try it (if the need arose) as it
is used in the CONCAT.  (Same question.)

--
Rich Ulrich


From: SPSSX(r) Discussion <[hidden email]> on behalf of Mario Giesel <[hidden email]>
Sent: Wednesday, April 8, 2020 10:45 PM
To: [hidden email] <[hidden email]>
Subject: Re: Looping Through A Data Set ?
 
There's an ambiguity in your code when you say:
define PeriodType () "AB" !enddefine.

and later you say:
do if PeriodType = "AB".

and then you say:
compute TwelveMoCohort = concat(PeriodType,YYstr).

I guess the latter two commands refer to a variable in your database.
If this is the case (otherwise your code wouldn't make sense  ...


===================== 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: Looping Through A Data Set ?

spss.giesel@yahoo.de
Rich, I think it should work as well.
John's code was a bit confusing for me without a data example.
Personally, I try to avoid multiple macros to achieve one goal.
This is why I prefer multi arguments macros.

Mario Giesel
Munich, Germany


Am Donnerstag, 9. April 2020, 07:46:31 MESZ hat Rich Ulrich <[hidden email]> Folgendes geschrieben:


Mario,

I'm curious - I have never used a Macro as a value being
tested with DO IF, but I can imagine that it could work.
I picture the value of the Macro being substituted, then used.
Do you know that it does not work, or is this an assumption?

I've used a Macro as a list of variables for an argument,
so I would be more likely to try it (if the need arose) as it
is used in the CONCAT.  (Same question.)

--
Rich Ulrich


From: SPSSX(r) Discussion <[hidden email]> on behalf of Mario Giesel <[hidden email]>
Sent: Wednesday, April 8, 2020 10:45 PM
To: [hidden email] <[hidden email]>
Subject: Re: Looping Through A Data Set ?
 
There's an ambiguity in your code when you say:
define PeriodType () "AB" !enddefine.

and later you say:
do if PeriodType = "AB".

and then you say:
compute TwelveMoCohort = concat(PeriodType,YYstr).

I guess the latter two commands refer to a variable in your database.
If this is the case (otherwise your code wouldn't make sense  ...


===================== 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: Looping Through A Data Set ?

Art Kendall
In reply to this post by SUBSCRIBE SPSSX-JohnF
I am not sure exactly what you are trying to do.

Isn't there something like SPSSINC PROCESS FILES?



-----
Art Kendall
Social Research Consultants
--
Sent from: http://spssx-discussion.1045642.n5.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
Art Kendall
Social Research Consultants
Reply | Threaded
Open this post in threaded view
|

Re: Looping Through A Data Set ?

SUBSCRIBE SPSSX-JohnF
In reply to this post by SUBSCRIBE SPSSX-JohnF
I really appreciate your efforts to assist me with this.......Thank You !

Below is data and the code.

DATA LIST LIST
 / DtReportBeg (ADate10) DtReportEnd (ADate10).
BEGIN DATA.
10/01/2012 03/31/2013
04/01/2013 09/30/2013
10/01/2012 03/31/2013
10/01/2013 03/31/2014
04/01/2014 09/30/2014
04/01/2014 09/30/2014
10/01/2014 03/31/2015
04/01/2015 09/30/2015
10/01/2014 03/31/2015
10/01/2015 03/31/2016
04/01/2016 09/30/2016
04/01/2016 09/30/2016
10/01/2016 03/31/2017
04/01/2017 09/30/2017
10/01/2016 03/31/2017
10/01/2017 03/31/2018
10/01/2017 03/31/2018
04/01/2018 09/30/2018
10/01/2018 03/31/2019
04/01/2019 09/30/2019
10/01/2018 03/31/2019
END DATA.
**//Once the AB or BA is decided it will not change from year to year.  In other words when
**//running a set of annual files you will always run 2015AB, 2016AB....2019AB or 2015BA, 2016BA....2019BA.
**//Or whatever sets of years you are running, 2013 thru 2017 or 2016 thru 2020.
define PeriodType () "AB"
!enddefine.
define YYYY () 2015.  
!enddefine.
define YYstr () "15"
!enddefine.

do if PeriodType = "AB".
compute DtPeriodBeg=date.mdy(10,01,YYYY-1).
compute DtPeriodEnd=date.mdy(09,30,YYYY).
compute DtMeasureEnd=date.mdy(09,30,YYYY+3).
compute DtMeasureEnd_Minus6Mo=date.mdy(03,31,YYYY+3).
end if.
do if PeriodType = "BA".
compute DtPeriodBeg=date.mdy(04,01,YYYY-1).
compute DtPeriodEnd=date.mdy(03,31,YYYY).
compute DtMeasureEnd=date.mdy(03,31,YYYY+3).
compute DtMeasureEnd_Minus6Mo=date.mdy(09,30,YYYY+2).
end if.
execute.
formats DtPeriodBeg DtPeriodEnd DtMeasureEnd DtMeasureEnd_Minus6Mo (adate10).

if (DtReportBeg ge DtPeriodBeg) and (DtReportEnd le DtMeasureEnd) ReportedDuringPeriod = 1.
execute.

select if ReportedDuringPeriod=1.
execute.

delete variables ReportedDuringPeriod.

string TwelveMoCohort (A4).
compute TwelveMoCohort = concat(PeriodType,YYstr).
execute.

save outfile='C:\Data\Total_'+ PeriodType + YYstr + '.sav'.
exe.

=====================
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: Looping Through A Data Set ?

spss.giesel@yahoo.de
Did you try version 2 of the macro?

Mario Giesel
Munich, Germany


Am Donnerstag, 9. April 2020, 23:10:25 MESZ hat SUBSCRIBE SPSSX-JohnF <[hidden email]> Folgendes geschrieben:


I really appreciate your efforts to assist me with this.......Thank You !

Below is data and the code.

DATA LIST LIST
/ DtReportBeg (ADate10) DtReportEnd (ADate10).
BEGIN DATA.
10/01/2012 03/31/2013
04/01/2013 09/30/2013
10/01/2012 03/31/2013
10/01/2013 03/31/2014
04/01/2014 09/30/2014
04/01/2014 09/30/2014
10/01/2014 03/31/2015
04/01/2015 09/30/2015
10/01/2014 03/31/2015
10/01/2015 03/31/2016
04/01/2016 09/30/2016
04/01/2016 09/30/2016
10/01/2016 03/31/2017
04/01/2017 09/30/2017
10/01/2016 03/31/2017
10/01/2017 03/31/2018
10/01/2017 03/31/2018
04/01/2018 09/30/2018
10/01/2018 03/31/2019
04/01/2019 09/30/2019
10/01/2018 03/31/2019
END DATA.
**//Once the AB or BA is decided it will not change from year to year.  In other words when
**//running a set of annual files you will always run 2015AB, 2016AB....2019AB or 2015BA, 2016BA....2019BA.
**//Or whatever sets of years you are running, 2013 thru 2017 or 2016 thru 2020.
define PeriodType () "AB"
!enddefine.
define YYYY () 2015. 
!enddefine.
define YYstr () "15"
!enddefine.

do if PeriodType = "AB".
compute DtPeriodBeg=date.mdy(10,01,YYYY-1).
compute DtPeriodEnd=date.mdy(09,30,YYYY).
compute DtMeasureEnd=date.mdy(09,30,YYYY+3).
compute DtMeasureEnd_Minus6Mo=date.mdy(03,31,YYYY+3).
end if.
do if PeriodType = "BA".
compute DtPeriodBeg=date.mdy(04,01,YYYY-1).
compute DtPeriodEnd=date.mdy(03,31,YYYY).
compute DtMeasureEnd=date.mdy(03,31,YYYY+3).
compute DtMeasureEnd_Minus6Mo=date.mdy(09,30,YYYY+2).
end if.
execute.
formats DtPeriodBeg DtPeriodEnd DtMeasureEnd DtMeasureEnd_Minus6Mo (adate10).

if (DtReportBeg ge DtPeriodBeg) and (DtReportEnd le DtMeasureEnd) ReportedDuringPeriod = 1.
execute.

select if ReportedDuringPeriod=1.
execute.

delete variables ReportedDuringPeriod.

string TwelveMoCohort (A4).
compute TwelveMoCohort = concat(PeriodType,YYstr).
execute.

save outfile='C:\Data\Total_'+ PeriodType + YYstr + '.sav'.
exe.

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

Re: Looping Through A Data Set ?

SUBSCRIBE SPSSX-JohnF
In reply to this post by SUBSCRIBE SPSSX-JohnF
Thanks.....I did and I couldn't get it to work.  It is very possible I am just not knowledgeable enough about macros which very likely the case.

=====================
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: Looping Through A Data Set ?

spss.giesel@yahoo.de
Can you please send the code you tried to run?

Mario Giesel
Munich, Germany


Am Freitag, 10. April 2020, 15:10:02 MESZ hat SUBSCRIBE SPSSX-JohnF <[hidden email]> Folgendes geschrieben:


Thanks.....I did and I couldn't get it to work.  It is very possible I am just not knowledgeable enough about macros which very likely the case.

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

Re: Looping Through A Data Set ?

SUBSCRIBE SPSSX-JohnF
In reply to this post by SUBSCRIBE SPSSX-JohnF
Thanks again for your interest, I do appreciate it. Below is the code you provided and I attempted to run.


If it does not work then try (I did try this approach also)
!PeriodType
instead of
PeriodType
within the macro.


***** MACRO START.
DEFINE !dostuff(PeriodType = !CHAREND('/')
        /Ystart = !TOKENS(1)
        /Yfinish = !TOKENS(1))
PRESERVE.
SET PRINTBACK = ON MPRINT = ON /* for debugging purposes */.
get file 'C:\Source data.sav'.
dataset name Indicator window=front.

!DO !y = !Ystart !TO !Yfinish
- do if (PeriodType = "AB").
- compute DtPeriodBeg=date.mdy(10,01,!y-1).
- compute DtPeriodEnd=date.mdy(09,30,!y).
- compute DtMeasureEnd=date.mdy(09,30,!y+3).
- compute DtMeasureEnd_Minus6Mo=date.mdy(03,31,!y+3).
- end if.

- do if (PeriodType = "BA").
- compute DtPeriodBeg=date.mdy(04,01,!y-1).
- compute DtPeriodEnd=date.mdy(03,31,!y).
- compute DtMeasureEnd=date.mdy(03,31,!y+3).
- compute DtMeasureEnd_Minus6Mo=date.mdy(09,30,!y+2).
- end if.

- formats DtPeriodBeg DtPeriodEnd DtMeasureEnd DtMeasureEnd_Minus6Mo (adate10).
- if (DtReportBeg ge DtPeriodBeg) and (DtReportEnd le DtMeasureEnd) ReportedDuringPeriod = 1.
- select if ReportedDuringPeriod=1.
- execute.
- delete variables ReportedDuringPeriod.
- string TwelveMoCohort (A4).
- compute TwelveMoCohort = concat(PeriodType,!SUBSTR(!y,3)).
- execute.
- save outfile=!CONCAT('C:\Data\Total_', !PeriodType, !SUBSTR(!y,3), '.sav').
!DOEND
RESTORE.
!ENDDEFINE.

* Calls.
!dostuff PeriodType = AB / Ystart = 2015 Yfinish = 2019.
!dostuff PeriodType = BA / Ystart = 2015 Yfinish = 2019.
***** MACRO END.

=====================
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: Looping Through A Data Set ?

spss.giesel@yahoo.de
Okay. There were some quotation issues.
Now try this:

* Encoding: UTF-8.

***** MACRO START.
DEFINE !dostuff(PeriodType = !CHAREND('/')
    /Ystart = !TOKENS(1)
    /Yfinish = !TOKENS(1))
PRESERVE.
SET PRINTBACK = ON MPRINT = ON /* for debugging purposes */.
get file 'mypath\sourcedata.sav'.
dataset name Indicator window=front.

!DO !y = !Ystart !TO !Yfinish
- do if (!QUOTE(!PeriodType) = "AB").
- compute DtPeriodBeg=date.mdy(10,01,!y-1).
- compute DtPeriodEnd=date.mdy(09,30,!y).
- compute DtMeasureEnd=date.mdy(09,30,!y+3).
- compute DtMeasureEnd_Minus6Mo=date.mdy(03,31,!y+3).
- end if.

- do if (!QUOTE(!PeriodType) = "BA").
- compute DtPeriodBeg=date.mdy(04,01,!y-1).
- compute DtPeriodEnd=date.mdy(03,31,!y).
- compute DtMeasureEnd=date.mdy(03,31,!y+3).
- compute DtMeasureEnd_Minus6Mo=date.mdy(09,30,!y+2).
- end if.

- formats DtPeriodBeg DtPeriodEnd DtMeasureEnd DtMeasureEnd_Minus6Mo (adate10).
- if (DtReportBeg ge DtPeriodBeg) and (DtReportEnd le DtMeasureEnd) ReportedDuringPeriod = 1.
- select if ReportedDuringPeriod=1.
- execute.
- delete variables ReportedDuringPeriod.
!IF (!y = !Ystart) !THEN 
- string TwelveMoCohort (A4).
!IFEND
- compute TwelveMoCohort = concat(!QUOTE(!PeriodType),!QUOTE(!SUBSTR(!y,3))).
- execute.
- save outfile=!QUOTE(!CONCAT('mypath\Total_', !PeriodType, !SUBSTR(!y,3), '.sav')).

!IF (!y = !Yfinish) !THEN
- get file 'mypath\sourcedata.sav'.
- dataset name Indicator window=front.
!IFEND
!DOEND
RESTORE.
!ENDDEFINE.
***** MACRO END.


DATA LIST LIST
/ DtReportBeg (ADate10) DtReportEnd (ADate10).
BEGIN DATA.
10/01/2012 03/31/2013
04/01/2013 09/30/2013
10/01/2012 03/31/2013
10/01/2013 03/31/2014
04/01/2014 09/30/2014
04/01/2014 09/30/2014
10/01/2014 03/31/2015
04/01/2015 09/30/2015
10/01/2014 03/31/2015
10/01/2015 03/31/2016
04/01/2016 09/30/2016
04/01/2016 09/30/2016
10/01/2016 03/31/2017
04/01/2017 09/30/2017
10/01/2016 03/31/2017
10/01/2017 03/31/2018
10/01/2017 03/31/2018
04/01/2018 09/30/2018
10/01/2018 03/31/2019
04/01/2019 09/30/2019
10/01/2018 03/31/2019
END DATA.

FILE HANDLE mypath /NAME = "your working directory" /*pls. edit*/.
SAVE OUTFILE = "mypath\sourcedata.sav".


* Calls.
!dostuff PeriodType = AB / Ystart = 2015 Yfinish = 2019.
!dostuff PeriodType = BA / Ystart = 2015 Yfinish = 2019.


Mario Giesel
Munich, Germany


Am Freitag, 10. April 2020, 19:25:25 MESZ hat SUBSCRIBE SPSSX-JohnF <[hidden email]> Folgendes geschrieben:


Thanks again for your interest, I do appreciate it. Below is the code you provided and I attempted to run.


If it does not work then try (I did try this approach also)
!PeriodType
instead of
PeriodType
within the macro.


***** MACRO START.
DEFINE !dostuff(PeriodType = !CHAREND('/')
    /Ystart = !TOKENS(1)
    /Yfinish = !TOKENS(1))
PRESERVE.
SET PRINTBACK = ON MPRINT = ON /* for debugging purposes */.
get file 'C:\Source data.sav'.
dataset name Indicator window=front.

!DO !y = !Ystart !TO !Yfinish
- do if (PeriodType = "AB").
- compute DtPeriodBeg=date.mdy(10,01,!y-1).
- compute DtPeriodEnd=date.mdy(09,30,!y).
- compute DtMeasureEnd=date.mdy(09,30,!y+3).
- compute DtMeasureEnd_Minus6Mo=date.mdy(03,31,!y+3).
- end if.

- do if (PeriodType = "BA").
- compute DtPeriodBeg=date.mdy(04,01,!y-1).
- compute DtPeriodEnd=date.mdy(03,31,!y).
- compute DtMeasureEnd=date.mdy(03,31,!y+3).
- compute DtMeasureEnd_Minus6Mo=date.mdy(09,30,!y+2).
- end if.

- formats DtPeriodBeg DtPeriodEnd DtMeasureEnd DtMeasureEnd_Minus6Mo (adate10).
- if (DtReportBeg ge DtPeriodBeg) and (DtReportEnd le DtMeasureEnd) ReportedDuringPeriod = 1.
- select if ReportedDuringPeriod=1.
- execute.
- delete variables ReportedDuringPeriod.
- string TwelveMoCohort (A4).
- compute TwelveMoCohort = concat(PeriodType,!SUBSTR(!y,3)).
- execute.
- save outfile=!CONCAT('C:\Data\Total_', !PeriodType, !SUBSTR(!y,3), '.sav').
!DOEND
RESTORE.
!ENDDEFINE.

* Calls.
!dostuff PeriodType = AB / Ystart = 2015 Yfinish = 2019.
!dostuff PeriodType = BA / Ystart = 2015 Yfinish = 2019.
***** MACRO END.

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

Re: Looping Through A Data Set ?

spss.giesel@yahoo.de
In reply to this post by SUBSCRIBE SPSSX-JohnF


Am Donnerstag, 9. April 2020, 23:10:25 MESZ hat SUBSCRIBE SPSSX-JohnF <[hidden email]> Folgendes geschrieben:


I really appreciate your efforts to assist me with this.......Thank You !

Below is data and the code.

DATA LIST LIST
/ DtReportBeg (ADate10) DtReportEnd (ADate10).
BEGIN DATA.
10/01/2012 03/31/2013
04/01/2013 09/30/2013
10/01/2012 03/31/2013
10/01/2013 03/31/2014
04/01/2014 09/30/2014
04/01/2014 09/30/2014
10/01/2014 03/31/2015
04/01/2015 09/30/2015
10/01/2014 03/31/2015
10/01/2015 03/31/2016
04/01/2016 09/30/2016
04/01/2016 09/30/2016
10/01/2016 03/31/2017
04/01/2017 09/30/2017
10/01/2016 03/31/2017
10/01/2017 03/31/2018
10/01/2017 03/31/2018
04/01/2018 09/30/2018
10/01/2018 03/31/2019
04/01/2019 09/30/2019
10/01/2018 03/31/2019
END DATA.
**//Once the AB or BA is decided it will not change from year to year.  In other words when
**//running a set of annual files you will always run 2015AB, 2016AB....2019AB or 2015BA, 2016BA....2019BA.
**//Or whatever sets of years you are running, 2013 thru 2017 or 2016 thru 2020.
define PeriodType () "AB"
!enddefine.
define YYYY () 2015. 
!enddefine.
define YYstr () "15"
!enddefine.

do if PeriodType = "AB".
compute DtPeriodBeg=date.mdy(10,01,YYYY-1).
compute DtPeriodEnd=date.mdy(09,30,YYYY).
compute DtMeasureEnd=date.mdy(09,30,YYYY+3).
compute DtMeasureEnd_Minus6Mo=date.mdy(03,31,YYYY+3).
end if.
do if PeriodType = "BA".
compute DtPeriodBeg=date.mdy(04,01,YYYY-1).
compute DtPeriodEnd=date.mdy(03,31,YYYY).
compute DtMeasureEnd=date.mdy(03,31,YYYY+3).
compute DtMeasureEnd_Minus6Mo=date.mdy(09,30,YYYY+2).
end if.
execute.
formats DtPeriodBeg DtPeriodEnd DtMeasureEnd DtMeasureEnd_Minus6Mo (adate10).

if (DtReportBeg ge DtPeriodBeg) and (DtReportEnd le DtMeasureEnd) ReportedDuringPeriod = 1.
execute.

select if ReportedDuringPeriod=1.
execute.

delete variables ReportedDuringPeriod.

string TwelveMoCohort (A4).
compute TwelveMoCohort = concat(PeriodType,YYstr).
execute.

save outfile='C:\Data\Total_'+ PeriodType + YYstr + '.sav'.
exe.

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