Automate Variable Name Changes

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

Automate Variable Name Changes

SUBSCRIBE SPSSX-JohnF
I've searched these boards for an answer to the following, without any luck.  Any response will be appreciated.  I have a Census file and the file looks like data list presented below....The real file has five years (2015 thru 2019) but I am trying to keep things simple.  I do more processing of the file than presented here, more than one aggregation, some recoding etc.  

What I am trying to do is when I get another year of data I'd like to change the years in one place rather than search and replace the name of each POPulation variable throughout the code.  Thanks for any assistance....John

Data List /State 1-2 Race_Ethnicity 4 ChildPOP.2015 6-13 ChildPOP.2016 15-22 ChildPOP.2017 24-31.
Begin Data
1  1 5340        5340        5430
1  2 5340        5340        5340
1  3 5340        5340        5340
1  4 5340        5340        5340
1  5 5340        5340        5340
1  6 5340        5340        5340
1  7 53400009 53404589 5340
2  1 5340        5340        5340
2  2 5340        5340        5340
2  3 5340        5340        5340
2  4 5340        5340        5340
2  5 5340        5340        5340
2  6 5340        5340        5340
2  7 5340        5340        5340
End data.

 AGGREGATE
  /OUTFILE=* MODE=ADDVARIABLES
  /PRESORTED
  /BREAK=state
  /TotalChildPOP.1=SUM(ChildPOP.2015)
  /TotalChildPOP.2=SUM(ChildPOP.2016)
  /TotalChildPOP.3=SUM(ChildPOP.2017).

=====================
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: Automate Variable Name Changes

Jon Peck
Why not just define a macro for the data portion and generate variable names from that?

On Fri, Jan 15, 2021 at 12:44 PM SUBSCRIBE SPSSX-JohnF <[hidden email]> wrote:
I've searched these boards for an answer to the following, without any luck.  Any response will be appreciated.  I have a Census file and the file looks like data list presented below....The real file has five years (2015 thru 2019) but I am trying to keep things simple.  I do more processing of the file than presented here, more than one aggregation, some recoding etc. 

What I am trying to do is when I get another year of data I'd like to change the years in one place rather than search and replace the name of each POPulation variable throughout the code.  Thanks for any assistance....John

Data List /State 1-2 Race_Ethnicity 4 ChildPOP.2015 6-13 ChildPOP.2016 15-22 ChildPOP.2017 24-31.
Begin Data
1  1 5340        5340        5430
1  2 5340        5340        5340
1  3 5340        5340        5340
1  4 5340        5340        5340
1  5 5340        5340        5340
1  6 5340        5340        5340
1  7 53400009 53404589 5340
2  1 5340        5340        5340
2  2 5340        5340        5340
2  3 5340        5340        5340
2  4 5340        5340        5340
2  5 5340        5340        5340
2  6 5340        5340        5340
2  7 5340        5340        5340
End data.

 AGGREGATE
  /OUTFILE=* MODE=ADDVARIABLES
  /PRESORTED
  /BREAK=state
  /TotalChildPOP.1=SUM(ChildPOP.2015)
  /TotalChildPOP.2=SUM(ChildPOP.2016)
  /TotalChildPOP.3=SUM(ChildPOP.2017).

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


--
Jon K Peck
[hidden email]

===================== 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: Automate Variable Name Changes

spss.giesel@yahoo.de
Like so?

* =================================================================================.
DEFINE !agg(!POS !CMDEND)
PRESERVE.
SET MPRINT = ON PRINTBACK = ON.
AGGREGATE
  /OUTFILE=* MODE=ADDVARIABLES OVERWRITE = YES
  /PRESORTED
  /BREAK=state
!LET !width = !NULL
!DO !year !IN (!1)
    !LET !width = !CONCAT(!width,x)
    !LET !n = !LENGTH(!width)
    !LET !subcommand = !CONCAT("/TotalChildPOP.",!n," = SUM(ChildPOP.",!year,")")
    !subcommand
!DOEND  
.
RESTORE.
!ENDDEFINE.

* Call macro with years as variadic arguments.
!agg 2015.
!agg 2015 2016.
!agg 2015 2016 2017.

* =================================================================================.

Mario Giesel
Munich, Germany

Am Samstag, 16. Januar 2021, 03:27:09 MEZ hat Jon Peck <[hidden email]> Folgendes geschrieben:


Why not just define a macro for the data portion and generate variable names from that?

On Fri, Jan 15, 2021 at 12:44 PM SUBSCRIBE SPSSX-JohnF <[hidden email]> wrote:
I've searched these boards for an answer to the following, without any luck.  Any response will be appreciated.  I have a Census file and the file looks like data list presented below....The real file has five years (2015 thru 2019) but I am trying to keep things simple.  I do more processing of the file than presented here, more than one aggregation, some recoding etc. 

What I am trying to do is when I get another year of data I'd like to change the years in one place rather than search and replace the name of each POPulation variable throughout the code.  Thanks for any assistance....John

Data List /State 1-2 Race_Ethnicity 4 ChildPOP.2015 6-13 ChildPOP.2016 15-22 ChildPOP.2017 24-31.
Begin Data
1  1 5340        5340        5430
1  2 5340        5340        5340
1  3 5340        5340        5340
1  4 5340        5340        5340
1  5 5340        5340        5340
1  6 5340        5340        5340
1  7 53400009 53404589 5340
2  1 5340        5340        5340
2  2 5340        5340        5340
2  3 5340        5340        5340
2  4 5340        5340        5340
2  5 5340        5340        5340
2  6 5340        5340        5340
2  7 5340        5340        5340
End data.

 AGGREGATE
  /OUTFILE=* MODE=ADDVARIABLES
  /PRESORTED
  /BREAK=state
  /TotalChildPOP.1=SUM(ChildPOP.2015)
  /TotalChildPOP.2=SUM(ChildPOP.2016)
  /TotalChildPOP.3=SUM(ChildPOP.2017).

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


--
Jon K Peck
[hidden email]

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