I have a string composed of codes separated by a space (the string also
has a leading space). I need to calculate the number of times a certain set of codes occur within the string for each case (or create a 0,1 flag when the condition of multiples within the set occur). I've used the INDEX syntax to identify when a specific code exist but not certain how to create a count when multiples exist. example string: A3 NG5 QA1 GUGY QA3 GY VT NA3 D4 TY QA1 LP A3 FT GYUP VT QA1 NA3 QA2 BH TY LP A1 D4 For the example, let's say the following codes are what we want to identify as the set (A3, NA3, QA1, QA3, D4), I've placed in ( ) the count I would need (or simply make it a 0,1 or Y/N). A3 NG5 QA1 GUGY (2) QA3 GY VT NA3 (2) D4 TY QA1 LP A3 (3) FT GYUP VT (0) QA1 NA3 (2) QA2 (0) BH TY LP A1 D4 (1) Thanks in advance. David ===================== 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 |
Administrator
|
The examples you show never have the same code within a string. Assuming that is always the case, something like the following (untested) computation should work.
compute kount = (index(stringvar," A3") GT 0) + (index(stringvar," NA3") GT 0) + (index(stringvar," QA1") GT 0) + (index(stringvar," QA3") GT 0) + (index(stringvar," D4") GT 0) . Or if you really like typing, you could use CHAR.INDEX, the new-fangled version of INDEX. ;-) HTH.
--
Bruce Weaver bweaver@lakeheadu.ca http://sites.google.com/a/lakeheadu.ca/bweaver/ "When all else fails, RTFM." PLEASE NOTE THE FOLLOWING: 1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above. 2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (https://listserv.uga.edu/). |
Administrator
|
Here is an untested MACRO which should do the trick. If it FUBARS then double check for matching parentheses. I'm on my Mac side right now and don't intend to spend 5 minutes to boot into Windoze. If at first you don't comprehend what this is doing then consult the FM re DEFINE !ENDDEFINE It is basically Bruce's code mapped into a List processing loop. ----- DEFINE SCount (STRVAR !TOKENS(1) / NEWVAR !TOKENS(1) / XARGS !ENCLOSE ("(",")") ) !LET !L= "" !DO !I !IN (!XARGS) !LET !L= !CONCAT(!L, "(INDEX(", !STRVAR, ",", !QUOTE (!CONCAT(" ",!I," ")), " GT 0 ), ") !DOEND !LET !L=!CONCAT(!L,"0") COMPUTE !NEWVAR = SUM(!L) !ENDDEFINE Scount STRVAR =STRINGVAR NEWVAR = NEWVAR XARGS (A3 NA3 QA1 QA3 D4).
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?" |
In reply to this post by wsu_wright
Thanks Bruce, this does what I need.
David, I didn't try your macro but have saved it for future needs. David On Thu, Jul 7, 2011 at 7:03 PM, Bruce Weaver wrote: > The examples you show never have the same code within a string. > Assuming > that is always the case, something like the following (untested) > computation > should work. > > compute kount = > (index(stringvar," A3") GT 0) + > (index(stringvar," NA3") GT 0) + > (index(stringvar," QA1") GT 0) + > (index(stringvar," QA3") GT 0) + > (index(stringvar," D4") GT 0) . > > Or if you really like typing, you could use CHAR.INDEX, the > new-fangled > version of INDEX. ;-) > > HTH. > > > David Wright-6 wrote: >> >> I have a string composed of codes separated by a space (the string >> also >> has a leading space). I need to calculate the number of times a >> certain >> set of codes occur within the string for each case (or create a 0,1 >> flag >> when the condition of multiples within the set occur). I've used the >> INDEX syntax to identify when a specific code exist but not certain >> how >> to create a count when multiples exist. >> >> example string: >> >> A3 NG5 QA1 GUGY >> QA3 GY VT NA3 >> D4 TY QA1 LP A3 >> FT GYUP VT >> QA1 NA3 >> QA2 >> BH TY LP A1 D4 >> >> >> For the example, let's say the following codes are what we want to >> identify as the set (A3, NA3, QA1, QA3, D4), I've placed in ( ) the >> count I would need (or simply make it a 0,1 or Y/N). >> >> A3 NG5 QA1 GUGY (2) >> QA3 GY VT NA3 (2) >> D4 TY QA1 LP A3 (3) >> FT GYUP VT (0) >> QA1 NA3 (2) >> QA2 (0) >> BH TY LP A1 D4 (1) >> >> >> Thanks in advance. >> >> David >> >> ===================== >> 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 >> > > > ----- > -- > Bruce Weaver > [hidden email] > http://sites.google.com/a/lakeheadu.ca/bweaver/ > > "When all else fails, RTFM." > > NOTE: My Hotmail account is not monitored regularly. > To send me an e-mail, please use the address shown above. > > -- > View this message in context: > http://spssx-discussion.1045642.n5.nabble.com/count-of-substr-entries-within-string-tp4562695p4562954.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 ===================== 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 |
In reply to this post by wsu_wright
At 05:42 PM 7/7/2011, David Wright wrote:
>I have a string composed of codes separated by a space (the string >also has a leading space). I need to calculate the number of times >a certain set of codes occur within the string for each case. Bruce's solution is a good one. If you want to write less code, though, DO REPEAT will work, and is probably easier to write than is a macro: COMPUTE kount = 0. DO REPEAT CODE= " A3"," NA3"," QA1"," QA3"," D4". . COMPUTE kount = kount + (index(stringvar,CODE) GT 0). END REPEAT. ===================== 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 |
In reply to this post by wsu_wright
Richard, Very nice, it does help reduce the syntax, especially when I
have several more code strings to count. Best wishes.... David On Fri, Jul 8, 2011 at 1:25 PM, Richard Ristow wrote: > At 05:42 PM 7/7/2011, David Wright wrote: > >> I have a string composed of codes separated by a space (the string >> also has a leading space). I need to calculate the number of times a >> certain set of codes occur within the string for each case. > > Bruce's solution is a good one. If you want to write less code, > though, DO REPEAT will work, and is probably easier to write than is a > macro: > > COMPUTE kount = 0. > DO REPEAT CODE= " A3"," NA3"," QA1"," QA3"," D4". > . COMPUTE kount = kount + (index(stringvar,CODE) GT 0). > END REPEAT. ===================== 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 |
Free forum by Nabble | Edit this page |