• Syntax to remove text from string - RTRIM?

    From erin.psychology@gmail.com@21:1/5 to All on Wed Jun 10 21:07:18 2020
    Hi there,
    My dataset has multiple responses and I just need the first response for analysis. So all text after the | characters can be deleted.

    I have 100 variables named OtherIssue_1 to OtherIssue_100 and would be good to have a loop rather than 100 pieces of syntax...but no idea how to do that.


    OtherIssue_1
    32
    36
    106|96
    88
    72|55

    I want to look like this:
    32
    36
    106
    88
    72

    Many thanks
    Erin

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bruce Weaver@21:1/5 to erin.ps...@gmail.com on Thu Jun 11 06:21:08 2020
    On Thursday, June 11, 2020 at 12:07:20 AM UTC-4, erin.ps...@gmail.com wrote:
    Hi there,
    My dataset has multiple responses and I just need the first response for analysis. So all text after the | characters can be deleted.

    I have 100 variables named OtherIssue_1 to OtherIssue_100 and would be good to have a loop rather than 100 pieces of syntax...but no idea how to do that.


    OtherIssue_1
    32
    36
    106|96
    88
    72|55

    I want to look like this:
    32
    36
    106
    88
    72

    Many thanks
    Erin

    There might be some neater method that is not occurring to me right now, but this works on some sample data similar to what you showed.

    * Version 1: Assuming you want new variables to be string.
    DATA LIST FREE / OtherIssue_1 OtherIssue_2 (2A10).
    BEGIN DATA
    "32"
    "36"
    "106|96"
    "88"
    "72|55"
    "9999|1234"
    END DATA.

    STRING new1 new2 (A10).
    DO REPEAT old = OtherIssue_1 TO OtherIssue_2 / new = new1 TO new2.
    COMPUTE #L = CHAR.INDEX(old,"|") - 1.
    IF #L EQ -1 #L = LENGTH(RTRIM(old)).
    COMPUTE new = CHAR.SUBSTR(old,1,#L).
    END REPEAT.
    LIST.


    * Version 2: Assuming you want new variables to be numeric.
    DATA LIST FREE / OtherIssue_1 OtherIssue_2 (2A10).
    BEGIN DATA
    "32"
    "36"
    "106|96"
    "88"
    "72|55"
    "9999|1234"
    END DATA.

    NUMERIC new1 new2 (F8.0).
    DO REPEAT old = OtherIssue_1 TO OtherIssue_2 / new = new1 TO new2.
    COMPUTE #L = CHAR.INDEX(old,"|") - 1.
    IF #L EQ -1 #L = LENGTH(RTRIM(old)).
    COMPUTE new = NUMBER(CHAR.SUBSTR(old,1,#L),F8).
    END REPEAT.
    LIST.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From erin.psychology@gmail.com@21:1/5 to All on Thu Jun 11 16:58:12 2020
    Thanks Bruce,
    That works. Any way to change it to override the existing responses rather than a new variable?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Rich Ulrich@21:1/5 to All on Thu Jun 11 20:47:42 2020
    On Thu, 11 Jun 2020 16:58:12 -0700 (PDT), erin.psychology@gmail.com
    wrote:

    Thanks Bruce,
    That works. Any way to change it to override the existing responses rather than a new variable?

    Please, don't do that.
    It is bad practice to re-use the same name for any variable
    when you substantially change it. Avoid confusion (and error)
    a few years later, or for when someone else uses the files.

    Going from alpha to numeric (chopping a value) is
    "substantial".

    Change the name, and write some in-line COMMENT for
    documentation, just to make it very explicit.

    If I loved the original names for later analyses, I would
    pre-pend a_ or A to the names in the file as received, so I
    could use that lovable set to receive the changed values.

    --
    Rich Ulrich

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bruce Weaver@21:1/5 to Rich Ulrich on Fri Jun 12 05:38:56 2020
    On Thursday, June 11, 2020 at 8:47:49 PM UTC-4, Rich Ulrich wrote:
    On Thu, 11 Jun 2020 16:58:12 -0700 (PDT), erin.psychology@gmail.com
    wrote:

    Thanks Bruce,
    That works. Any way to change it to override the existing responses rather than a new variable?

    Please, don't do that.
    It is bad practice to re-use the same name for any variable
    when you substantially change it. Avoid confusion (and error)
    a few years later, or for when someone else uses the files.

    Agreed!

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