counting values within a string variable

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

counting values within a string variable

Matthew Hoover
Hello listers,

I'm sure this question has been asked before, but my extensive google-fu skills isn't giving me the answer...

I have a variable called "quarters" that is a string variable.  It is a list of values within a bracket that correspond to the number of quarters that a person has been enrolled in college after completing a college skills program.  Responses could like like this:

{0,2,3,4,5,6}
{1,2,3,4,7}
{0,2,3,4,5,6,7,8,9,10,11,12,13,15}

As an FYI, a first value of zero means that they entered college in the same quarter as graduating a skills program and value of 1 means that they entered 1 quarter after graduating.  The rest of the numbers correspond to the quarters after graduating the program that they remained in college.  Missing number(s) in the sequence means that they left college over that time period.

All that I would like to do is count the number of instances that any number occurs (including zeros).  So for the first example, I'd like to create a variable that will count all valid numbers within the bracket and return a value of 6 (as there are 6 values within the bracket).  The second example would return a value of 5.  The third example would return a value of 14 (there are 14 values including the zero).

Is there a way of doing this?  I'm assuming it is some kind of loop, but I'm not just looking to count the number of instances of any particular value, but the number of actual valid numbers.

Hope this makes sense!  Thanks for you help :).

Matt


--
Matthew Hoover
Research Associate
Center for Youth and Communities
Heller School for Social Policy and Management
MS035 Brandeis University
415 South Street
Waltham, MA 02454
TEL 781.736.8631
FAX 781.736.3773
[hidden email]
"CYC: Making Knowledge Productive for 30 Years"
http://cyc.brandeis.edu/

===================== 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: counting values within a string variable

Maguin, Eugene

There certainly might be more clever ways but a crude way is

 

*  Assume that “{}” is not possible.

Loop #a=3 to char.length(quarters).

If (char.substr(quarters,#a,1) eq ‘,’) nquarters=sum(nquarters,1).

End loop.

Compute nquarters=sum(nquarters,1). /* case where “{2}”

 

Gene Maguin

 

 

 

 

From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Matthew Hoover
Sent: Friday, October 23, 2015 4:50 PM
To: [hidden email]
Subject: counting values within a string variable

 

Hello listers,

I'm sure this question has been asked before, but my extensive google-fu skills isn't giving me the answer...

I have a variable called "quarters" that is a string variable.  It is a list of values within a bracket that correspond to the number of quarters that a person has been enrolled in college after completing a college skills program.  Responses could like like this:

{0,2,3,4,5,6}
{1,2,3,4,7}
{0,2,3,4,5,6,7,8,9,10,11,12,13,15}

As an FYI, a first value of zero means that they entered college in the same quarter as graduating a skills program and value of 1 means that they entered 1 quarter after graduating.  The rest of the numbers correspond to the quarters after graduating the program that they remained in college.  Missing number(s) in the sequence means that they left college over that time period.

All that I would like to do is count the number of instances that any number occurs (including zeros).  So for the first example, I'd like to create a variable that will count all valid numbers within the bracket and return a value of 6 (as there are 6 values within the bracket).  The second example would return a value of 5.  The third example would return a value of 14 (there are 14 values including the zero).

Is there a way of doing this?  I'm assuming it is some kind of loop, but I'm not just looking to count the number of instances of any particular value, but the number of actual valid numbers.

Hope this makes sense!  Thanks for you help :).

Matt



--

Matthew Hoover
Research Associate
Center for Youth and Communities
Heller School for Social Policy and Management
MS035 Brandeis University
415 South Street
Waltham, MA 02454
TEL 781.736.8631
FAX 781.736.3773

[hidden email]
"CYC: Making Knowledge Productive for 30 Years"
http://cyc.brandeis.edu/

===================== 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: counting values within a string variable

David Marso
Administrator
Very similar...
DATA LIST / var (A80).
BEGIN DATA
{0,2,3,4,5,6}
{1,2,3,4,7}
{0,2,3,4,5,6,7,8,9,10,11,12,13,15}
END DATA.

LOOP #=1 TO CHAR.LENGTH(var).
COMPUTE n=SUM(n,CHAR.SUBSTR(var,#,1)EQ ',').
END LOOP.
COMPUTE n=SUM(n,1).
LIST.

Maguin, Eugene wrote
There certainly might be more clever ways but a crude way is

*  Assume that “{}” is not possible.
Loop #a=3 to char.length(quarters).
If (char.substr(quarters,#a,1) eq ‘,’) nquarters=sum(nquarters,1).
End loop.
Compute nquarters=sum(nquarters,1). /* case where “{2}”

Gene Maguin




From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Matthew Hoover
Sent: Friday, October 23, 2015 4:50 PM
To: [hidden email]
Subject: counting values within a string variable

Hello listers,
I'm sure this question has been asked before, but my extensive google-fu skills isn't giving me the answer...
I have a variable called "quarters" that is a string variable.  It is a list of values within a bracket that correspond to the number of quarters that a person has been enrolled in college after completing a college skills program.  Responses could like like this:

{0,2,3,4,5,6}
{1,2,3,4,7}
{0,2,3,4,5,6,7,8,9,10,11,12,13,15}
As an FYI, a first value of zero means that they entered college in the same quarter as graduating a skills program and value of 1 means that they entered 1 quarter after graduating.  The rest of the numbers correspond to the quarters after graduating the program that they remained in college.  Missing number(s) in the sequence means that they left college over that time period.
All that I would like to do is count the number of instances that any number occurs (including zeros).  So for the first example, I'd like to create a variable that will count all valid numbers within the bracket and return a value of 6 (as there are 6 values within the bracket).  The second example would return a value of 5.  The third example would return a value of 14 (there are 14 values including the zero).
Is there a way of doing this?  I'm assuming it is some kind of loop, but I'm not just looking to count the number of instances of any particular value, but the number of actual valid numbers.
Hope this makes sense!  Thanks for you help :).
Matt


--
Matthew Hoover
Research Associate
Center for Youth and Communities
Heller School for Social Policy and Management
MS035 Brandeis University
415 South Street
Waltham, MA 02454
TEL 781.736.8631
FAX 781.736.3773
[hidden email]<mailto:[hidden email]>
"CYC: Making Knowledge Productive for 30 Years"
http://cyc.brandeis.edu/
===================== To manage your subscription to SPSSX-L, send a message to [hidden email]<mailto:[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
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?"