• Count occurances of a charachter within a string

    From Erin Holloway@21:1/5 to All on Thu Feb 11 21:35:26 2021
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Rich Ulrich@21:1/5 to erin.psychology@gmail.com on Sun Feb 14 13:33:47 2021
    On Thu, 11 Feb 2021 21:35:26 -0800 (PST), Erin Holloway <erin.psychology@gmail.com> wrote:



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

    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Erin Holloway@21:1/5 to Rich Ulrich on Wed Feb 17 13:42:41 2021
    On Monday, February 15, 2021 at 5:33:53 AM UTC+11, Rich Ulrich wrote:
    On Thu, 11 Feb 2021 21:35:26 -0800 (PST), Erin Holloway <erin.ps...@gmail.com> wrote:



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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)