☑ This topic is solved.
Need further help from the community? Please
sign in and ask a new question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Posted 03-04-2024 02:38 PM
(959 views)
Hi there, I would like to use compress function to take away any substring of “1/2”, but I found that it also removes any “1” or “2” from the string. E.g I have “20 1/2 Apple” and I want it to be “20 Apple”, but when I tried to use the compress function it gives me “0 Apple”.. is there anyway to use compress function to achieve this?
This is my data:
Data have; input var 100$.;
Cards;
20 1/2 Apple
;
Run;
The code I tried is the following
var=compress(var, “1/2”) but it does not work.
This is what I want:
Data want; input var 100$.;
Cards;
20 Apple
;
Run;
This is my data:
Data have; input var 100$.;
Cards;
20 1/2 Apple
;
Run;
The code I tried is the following
var=compress(var, “1/2”) but it does not work.
This is what I want:
Data want; input var 100$.;
Cards;
20 Apple
;
Run;
1 ACCEPTED SOLUTION
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
you can use prxchange again.
new_string = prxchange('s/1\/2//', -1, text2);
4 REPLIES 4
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
you can use prxchange again.
new_string = prxchange('s/1\/2//', -1, text2);
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thanks for your help!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Read the documentation a bit more carefully:
COMPRESS Function
Returns a character string with specified characters removed from the original string (emphasis added for character not string of characters)
So Compress removes all the characters you provide.
If you want to replace a specific sequence of characters you might try TRANSTR
Data have; infile datalines truncover; input var $100.; want = transtrn(var,'1/2',''); Cards; 20 1/2 Apple ; Run;
Note changes to your data step to actually read the value of VAR.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
COMPRESS() is NOT what you want. That removes CHARACTERS.
So all of these statements are the same:
var=compress(var, '1/2') ;
var=compress(var, '12/') ;
var=compress(var, '/12') ;
var=compress(var, '21/') ;
You want TRANWRD() or TRANSTRN() instead. You can use COMPBL() collapse multiple adjacent spaces into just one space.
var=compbl(tranwrd(var,'1/2',' '));