Discussion:
Syntax to remove text from string - RTRIM?
(too old to reply)
e***@gmail.com
2020-06-11 04:07:18 UTC
Permalink
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
2020-06-11 13:21:08 UTC
Permalink
Post by e***@gmail.com
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
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.
e***@gmail.com
2020-06-11 23:58:12 UTC
Permalink
Thanks Bruce,
That works. Any way to change it to override the existing responses rather than a new variable?
Rich Ulrich
2020-06-12 00:47:42 UTC
Permalink
Post by e***@gmail.com
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
2020-06-12 12:38:56 UTC
Permalink
Post by Rich Ulrich
Post by e***@gmail.com
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!

Loading...