Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Syntax to remove text from string - RTRIM?

77 views
Skip to first unread message

erin.ps...@gmail.com

unread,
Jun 11, 2020, 12:07:20 AM6/11/20
to
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

Bruce Weaver

unread,
Jun 11, 2020, 9:21:10 AM6/11/20
to
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.

erin.ps...@gmail.com

unread,
Jun 11, 2020, 7:58:14 PM6/11/20
to
Thanks Bruce,
That works. Any way to change it to override the existing responses rather than a new variable?

Rich Ulrich

unread,
Jun 11, 2020, 8:47:49 PM6/11/20
to
On Thu, 11 Jun 2020 16:58:12 -0700 (PDT), erin.ps...@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

Bruce Weaver

unread,
Jun 12, 2020, 8:38:58 AM6/12/20
to
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.ps...@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!
0 new messages