Macro code.

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

Macro code.

galatios
Hello,

I have this little code for year 2010:


string var2010 (a50).
compute var2010=countriesin2010.
execute.

compute var1=CHAR.INDEX(var2010, ',').
string var2010_1 (a20).
compute var2010_1=CHAR.SUBSTR(var2010, 1, var1-1).
execute.


I need to do the same for all years until 2016.

Is it possible to use some loop, so I'll only have to write the code just
once instead of 6 times?

Regards, Galatios

=====================
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: Macro code.

Andy W
You could use DO REPEAT and scratch variables. Something like (untested).

VECTOR YearVar_(7,A20).
DO REPEAT C = countriesin2010 TO countriesin2016
         /Y = YearVar_1 TO YearVar_7.
  COMPUTE #Split = CHAR.INDEX(C,',').
  COMPUTE Y = CHAR.SUBSTR(C,1,#Split-1).
END REPEAT.

Then use RENAME VARIABLES if you want your YearVar_ vector to be a nicer set
of names corresponding to each year.
                 
Another way depending on your data flow would be to use VARSTOCASES on
'countriesin2010' -- 'countriesin2016', so all of the year variables are in
one column instead of in separate rows.



-----
Andy W
[hidden email]
http://andrewpwheeler.wordpress.com/
--
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
Andy W
apwheele@gmail.com
http://andrewpwheeler.wordpress.com/