Discussion:
Count occurances of a charachter within a string
(too old to reply)
Erin Holloway
2021-02-12 05:35:26 UTC
Permalink
Hi there,
I am trying to count the number of '~' in my string variable which looks like:

oos_service_dates_CONCAT
2020-10-20~2020-10-29~2020-11-05~2020-11-12~2020-12-03
2020-10-20~2020-10-27
2020-10-21

Essentially the plan is to count the dates by counting the number of deliniators (~) and then add one.

I have tried this, which runs, but every case is '0'
COUNT OoS_Count=oos_service_dates_CONCAT('~').

Any ideas??
Thanks in advance
Erin
Rich Ulrich
2021-02-14 18:33:47 UTC
Permalink
On Thu, 11 Feb 2021 21:35:26 -0800 (PST), Erin Holloway
Post by Erin Holloway
Hi there,
oos_service_dates_CONCAT
2020-10-20~2020-10-29~2020-11-05~2020-11-12~2020-12-03
2020-10-20~2020-10-27
2020-10-21
Essentially the plan is to count the dates by counting the number of deliniators (~) and then add one.
I have tried this, which runs, but every case is '0'
COUNT OoS_Count=oos_service_dates_CONCAT('~').
Any ideas??
Searching a single string variables would be a clever extenstion
of the COUNT command, but that is not how COUNT works. It
wants a set of variables, to compare to a value.

The way to search a string in SPSS syntax is to use char.index
and to find the matches one at a time, re-starting the search
after each match.

I suspect that counting matches in a string is available in the
Python extensions, which I don't know much about. For your
data, it probably can break out the dates, giving you a set of
new variables and a count of how many.

[cribbed from an older message by Bruce]

The SPSS mailing list (http://spssx-discussion.1045642.n5.nabble.com/)
is a lot more active than this forum these days. You might want to
join that list (if not already a member) and post your question there.
Via the page given above, click on -more options- near the top for
info on how to subscribe. HTH.
--
Rich Ulrich
Erin Holloway
2021-02-17 21:42:41 UTC
Permalink
Post by Rich Ulrich
On Thu, 11 Feb 2021 21:35:26 -0800 (PST), Erin Holloway
Post by Erin Holloway
Hi there,
oos_service_dates_CONCAT
2020-10-20~2020-10-29~2020-11-05~2020-11-12~2020-12-03
2020-10-20~2020-10-27
2020-10-21
Essentially the plan is to count the dates by counting the number of deliniators (~) and then add one.
I have tried this, which runs, but every case is '0'
COUNT OoS_Count=oos_service_dates_CONCAT('~').
Any ideas??
Searching a single string variables would be a clever extenstion
of the COUNT command, but that is not how COUNT works. It
wants a set of variables, to compare to a value.
The way to search a string in SPSS syntax is to use char.index
and to find the matches one at a time, re-starting the search
after each match.
I suspect that counting matches in a string is available in the
Python extensions, which I don't know much about. For your
data, it probably can break out the dates, giving you a set of
new variables and a count of how many.
[cribbed from an older message by Bruce]
The SPSS mailing list (http://spssx-discussion.1045642.n5.nabble.com/)
is a lot more active than this forum these days. You might want to
join that list (if not already a member) and post your question there.
Via the page given above, click on -more options- near the top for
info on how to subscribe. HTH.
--
Rich Ulrich
THanks Rich! Will do

Loading...