BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Feyng819
Obsidian | Level 7
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;


1 ACCEPTED SOLUTION

Accepted Solutions
tarheel13
Rhodochrosite | Level 12

you can use prxchange again. 

new_string = prxchange('s/1\/2//', -1, text2);

View solution in original post

4 REPLIES 4
tarheel13
Rhodochrosite | Level 12

you can use prxchange again. 

new_string = prxchange('s/1\/2//', -1, text2);
Feyng819
Obsidian | Level 7
Thanks for your help!
ballardw
Super User

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.

Tom
Super User Tom
Super User

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',' '));

 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 604 views
  • 4 likes
  • 4 in conversation