SPSS Macro

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

SPSS Macro

Scott Roesch
I hope someone can help my old eyes out.

I have written some code that basically takes multiple values in a cell (e.g., 1,2,3) and creates new variables for each individual value. The problem that I am encountering is that the code that I have written adds a comma at the end of the original value (e.g., 1,2,3,). Below is the code, any help would be greatly appreciated.


* Encoding: UTF-8.
DEFINE !parse (var=!TOKENS(1) /nbval=!TOKENS(1))
COMPUTE !var=CONCAT(RTRIM(!var),',').
STRING #str(A8).
VECTOR !var (!nbval F8.0).
COMPUTE #beg=1.
LOOP #cnt=1 TO !nbval.
+COMPUTE #str=CHAR.SUBSTR(!var,#beg).
+COMPUTE #end=CHAR.INDEX(#str,',')-1.
+DO IF #end=-1.
+ BREAK.
+END IF.
+COMPUTE !var(#cnt)=NUMBER(CHAR.SUBSTR(#str,1,#end),F8.0).
+COMPUTE #beg=#beg+#end.
END LOOP IF #end=-1.
EXECUTE.
!ENDDEFINE.

* Call the macro.
!parse var=var1 nbval=7.
!parse var=var2 nbval=9.

===================== 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: SPSS Macro

David Marso
Administrator
See the RTRIM function!
--

Scott Roesch wrote
I hope someone can help my old eyes out.

I have written some code that basically takes multiple values in a cell
(e.g., 1,2,3) and creates new variables for each individual value. The
problem that I am encountering is that the code that I have written adds a
comma at the end of the original value (e.g., 1,2,3,). Below is the code,
any help would be greatly appreciated.


* Encoding: UTF-8.
DEFINE !parse (var=!TOKENS(1) /nbval=!TOKENS(1))
COMPUTE !var=CONCAT(RTRIM(!var),',').
STRING #str(A8).
VECTOR !var (!nbval F8.0).
COMPUTE #beg=1.
LOOP #cnt=1 TO !nbval.
+COMPUTE #str=CHAR.SUBSTR(!var,#beg).
+COMPUTE #end=CHAR.INDEX(#str,',')-1.
+DO IF #end=-1.
+ BREAK.
+END IF.
+COMPUTE !var(#cnt)=NUMBER(CHAR.SUBSTR(#str,1,#end),F8.0).
+COMPUTE #beg=#beg+#end.
END LOOP IF #end=-1.
EXECUTE.
!ENDDEFINE.

* Call the macro.
!parse var=var1 nbval=7.
!parse var=var2 nbval=9.

J

=====================
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
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: SPSS Macro

Art Kendall
In reply to this post by Scott Roesch
Isn't there an extension command to exactly this?
Art Kendall
Social Research Consultants
Reply | Threaded
Open this post in threaded view
|

Re: SPSS Macro

Jon Peck
See SPSSINC CREATE DUMMIES (Transform > Create Dummy Variables).  It has a number of related features for dummy variable creation.

On Thu, Apr 6, 2017 at 5:43 AM, Art Kendall <[hidden email]> wrote:
Isn't there an extension command to exactly this?



-----
Art Kendall
Social Research Consultants
--
View this message in context: http://spssx-discussion.1045642.n5.nabble.com/SPSS-Macro-tp5734052p5734055.html
Sent from the SPSSX Discussion mailing list archive at 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



--
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: SPSS Macro

Kirill Orlov
In reply to this post by Scott Roesch
Visit my page http://www.spsstools.net/en/KO-spssmacros/

In collection Categorical-Binary Recodings there are macros
!catbin - recode nominal variable(s) to binary, dummy ones
!mrcmrd - recode MR set of categorical variables into MR dichotomous set
etc

In collection Multiple-Response Tools
!strmrc or !strmrd - create categorical MR set or dichotomous MR set from a string variable containing codes (there several modes codes packing are considered)


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