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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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